[R] Why mean is not working in by?
Bert Gunter
bgunter.4567 at gmail.com
Wed Dec 9 00:09:04 CET 2015
Because you are using by() incorrectly.
"A data frame is split by row into **data frames** subsetted by the
values of one or more factors, and function FUN is applied to each
subset in turn."
So your FUN is applied to a subset of the data frame (which is also a
list). Note that sum, min, and max have "..." as their initial
arguments and so use all the columns in the data frame of each subset
for .... var() takes the covariance matrix of the several columns and
summary.data.frame summarizes each column. mean() and sd() must be fed
a numeric vector as their first argument, which a data frame is not --
ergo the error.
Cheers,
Bert
Bert Gunter
"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
-- Clifford Stoll
On Tue, Dec 8, 2015 at 2:30 PM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> wrote:
> Hello!
> Could you please explain why the first 5 lines work but the last 2 lines don't?
> Thank you!
>
> by(data = iris[myvars], INDICES = iris["Species"], FUN = summary)
> by(data = iris[myvars], INDICES = iris["Species"], FUN = sum)
> by(data = iris[myvars], INDICES = iris["Species"], FUN = var)
> by(data = iris[myvars], INDICES = iris["Species"], FUN = max)
> by(data = iris[myvars], INDICES = iris["Species"], FUN = min)
>
> by(data = iris[myvars], INDICES = iris["Species"], FUN = sd)
> by(data = iris[myvars], INDICES = iris["Species"], FUN = mean)
>
> --
> Dimitri Liakhovitski
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
More information about the R-help
mailing list