Hi everybody
I'm having some unexpected errors from the 'by' function when using
it to
apply the mean function (by a factor) to a dataframe of numerical
variables. The following piece of toy code demonstrates:
myDataF<-warpbreaks
myDataF$X<-runif(54) # creating an additional numerical variable in
warpbreaks for purposes of this demo
by(myDataF[,c("X","breaks")],myDataF[,"tension"],FUN=mean)
In R3.0.1 & R3.0.2 (run on Windows 7) this gives the following error
myDataF[, "tension"]: L
[1] NA
-----------------------------------------------------------------
myDataF[, "tension"]: M
[1] NA
-----------------------------------------------------------------
myDataF[, "tension"]: H
[1] NA
Warning messages:
1: In mean.default(data[x, , drop = FALSE], ...) :
argument is not numeric or logical: returning NA
2: In mean.default(data[x, , drop = FALSE], ...) :
argument is not numeric or logical: returning NA
3: In mean.default(data[x, , drop = FALSE], ...) :
argument is not numeric or logical: returning NA
In R2.15.2 (again run on Windows 7) the works but gives a warning about a
depreciated function
: L
X breaks
0.5705876 36.3888889
-----------------------------------------------------------------
: M
X breaks
0.5370582 26.3888889
-----------------------------------------------------------------
: H
X breaks
0.3908149 21.6666667
Warning messages:
1: mean(<data.frame>) is deprecated.
Use colMeans() or sapply(*, mean) instead.
2: mean(<data.frame>) is deprecated.
Use colMeans() or sapply(*, mean) instead.
3: mean(<data.frame>) is deprecated.
Use colMeans() or sapply(*, mean) instead.
I *think* that in older versions of R (which I no longer have installed)
the use of 'by' in this way worked fine (it seems to appear in old code
of
mine, anyway). This would be consistent with the behavior being caused by
a depreciated function, I guess.
However, the help page for 'by' suggests that the above code should
work,
as far as I can tell, so I'm a bit confused - anybody got any ideas what
I'm doing wrong here, please?
Cheers,
Carina
[[alternative HTML version deleted]]