Re: [R] apply with a division

From: Henrique Dallazuanna <wwwhsd_at_gmail.com>
Date: Thu, 03 Jul 2008 19:15:41 -0300

Try this:

sweep(x, 2, as.numeric(x[1,]), FUN="/")

On Thu, Jul 3, 2008 at 7:04 PM, Greg Kettler <gkettler_at_gmail.com> wrote:

> Hi,
> I'd like to normalize a dataset by dividing each row by the first row.
> Very simple, right?
> I tried this:
>
> > expt.fluor
> X1 X2 X3
> 1 124 120 134
> 2 165 163 174
> 3 52 51 43
> 4 179 171 166
> 5 239 238 235
>

> >first.row <- expt.fluor[1,]
> > normed <- apply(expt.fluor, 1, function(r) {r / first.row})
> >normed
> [[1]]
> X1 X2 X3
> 1 1 1 1
>
> [[2]]
> X1 X2 X3
> 1 1.330645 1.358333 1.298507
>
> [[3]]
> X1 X2 X3
> 1 0.4193548 0.425 0.3208955
>
> [[4]]
> X1 X2 X3
> 1 1.443548 1.425 1.238806
>
> [[5]]
> X1 X2 X3
> 1 1.927419 1.983333 1.753731
>
> Ugly! The values are right, but why didn't I get another 2D array
> back? Shouldn't the division in my inline function return a vector?
>
> Thanks,
> Greg
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

	[[alternative HTML version deleted]]


______________________________________________ R-help_at_r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

Received on Thu 03 Jul 2008 - 22:18:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 03 Jul 2008 - 22:31:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive