[R] dividing values of each column in a dataframe
Berend Hasselman
bhh at xs4all.nl
Tue Feb 7 08:35:27 CET 2012
On 07-02-2012, at 00:09, Rolf Turner wrote:
>
> I believe your post is misleading. Your example "works"
> purely by chance.
>
> R uses "column ordering", so entries 1 to 3 of column 1 in your
> example get divided by 2, 3, and 4 respectively. Then "scal" is
> *recycled* and entries 4, 5, and 6, get divided by 2, 3, and 4
> respectively, and so on.
>
> It just *happens* that entry 4 of column 1 gets divided by 2,
> entry 4 of column 2 gets divided by 3, and entry 4 of column 3
> gets divided by 4, giving the impression that you are getting
> what you want. But if you look at row 5 of your "df" you'll see
> something different.
>
> # The same (due to serendipity --- or it's converse!):
> > (df/scal)[4,]
> a b c
> 4 0.0156342 0.03288247 0.02057588
> > df[4,]/scal
> a b c
> 4 0.0156342 0.03288247 0.02057588
>
> # Not the same!
> > (df/scal)[5,]
> a b c
> 5 0.07904605 0.01585556 0.4562085
> > df[5,]/scal
> a b c
> 5 0.1185691 0.02114075 0.2281042
>
> As has already been pointed out in this thread, to get what you think
> you're getting, you need to use transpose t(t(df)/scal).
You are completely correct.
I should have checked more thoroughly.
Using this dataframe makes the error glaring
df <- data.frame(a=rep(2,10),b=rep(3,10),c=rep(4,10))
My apologies to the OP and the list.
Berend Hasselman
More information about the R-help
mailing list