I am trying to reproduce the analysis given in this blog post for the by() function. When I paste the code into R I get an error message, however, instead of the nice table of summarised iris data on the blog post.
attach(iris)
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
So the data frame's there and all is well.
Pasting in the by() function from the blog gives me this error:
by(iris[, 1:4], Species, mean)
Species: setosa
[1] NA
----------------------------------------------------------------------------------
Species: versicolor
[1] NA
----------------------------------------------------------------------------------
Species: virginica
[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
I really can't see what's wrong here. I've tried it with other data frames and so on and the problem seems to be with the 1:4 sequence in the indexing for the data frame. If I just specify one column it gives me the means no problem. I can't work out why it's spitting its dummy when given more than one column. Any suggestions?