Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- > 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
- R> mean(iris[iris$Species == "setosa", 1:4])
- [1] NA
- Warning message:
- In mean.default(iris[iris$Species == "setosa", 1:4]) :
- argument is not numeric or logical: returning NA
- R> by(iris[,1] , iris$Species , mean)
- iris$Species: setosa
- [1] 5.006
- ------------------------------------------------------------
- iris$Species: versicolor
- [1] 5.936
- ------------------------------------------------------------
- iris$Species: virginica
- [1] 6.588
- R> by(iris[,1:4] , iris$Species , colMeans)
- iris$Species: setosa
- Sepal.Length Sepal.Width Petal.Length Petal.Width
- 5.006 3.428 1.462 0.246
- ------------------------------------------------------------
- iris$Species: versicolor
- Sepal.Length Sepal.Width Petal.Length Petal.Width
- 5.936 2.770 4.260 1.326
- ------------------------------------------------------------
- iris$Species: virginica
- Sepal.Length Sepal.Width Petal.Length Petal.Width
- 6.588 2.974 5.552 2.026
- foo <- function(x, ...) sapply(x, mean, ...)
- by(iris[, 1:4], iris$Species, foo)
- R> by(iris[, 1:4], iris$Species, foo)
- iris$Species: setosa
- Sepal.Length Sepal.Width Petal.Length Petal.Width
- 5.006 3.428 1.462 0.246
- ------------------------------------------------------------
- iris$Species: versicolor
- Sepal.Length Sepal.Width Petal.Length Petal.Width
- 5.936 2.770 4.260 1.326
- ------------------------------------------------------------
- iris$Species: virginica
- Sepal.Length Sepal.Width Petal.Length Petal.Width
- 6.588 2.974 5.552 2.026
- R> with(iris, aggregate(iris[,1:4], list(Species = Species), FUN = mean))
- Species Sepal.Length Sepal.Width Petal.Length Petal.Width
- 1 setosa 5.006 3.428 1.462 0.246
- 2 versicolor 5.936 2.770 4.260 1.326
- 3 virginica 6.588 2.974 5.552 2.026
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement