[R] Why mean is not working in by?

Sarah Goslee sarah.goslee at gmail.com
Tue Dec 8 23:50:54 CET 2015


Hi Dimitri,

I changed this into a reproducible example (we don't know what myvars
is). Assuming length(myvars) > 1, I'm not convinced that your first
five lines "work" either: what do you expect?

I get:

> by(data = iris[, -5], INDICES = iris["Species"], FUN = min)
Species: setosa
[1] 0.1
------------------------------------------------------------------
Species: versicolor
[1] 1
------------------------------------------------------------------
Species: virginica
[1] 1.4

But was expecting:

> aggregate(iris[,-5], by=iris[,"Species", drop=FALSE], FUN=min)
     Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1     setosa          4.3         2.3          1.0         0.1
2 versicolor          4.9         2.0          3.0         1.0
3  virginica          4.9         2.2          4.5         1.4



aggregate(iris[,-5], by=iris[,"Species", drop=FALSE], FUN=sd)
aggregate(iris[,-5], by=iris[,"Species", drop=FALSE], FUN=mean)

provide the answers I would expect. If you want clearer advice, you
need to provide an actually reproducible example, and tell us more
about what you expect to get.

Sarah


On Tue, Dec 8, 2015 at 5: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
>



More information about the R-help mailing list