【发布时间】:2015-09-01 22:22:42
【问题描述】:
输入:
data(iris)
tapply(iris$Sepal.Length, iris$Species, mean)
tapply(iris$Sepal.Length, iris$Species, median)
期望的输出:显示以下内容的数据集
#setosa versicolor virginica
#5.006 5.936 6.588
#5.0 5.9 6.5
创建包含各种 tapply() 输出的新的单一数据集的最佳方法是什么?
【问题讨论】:
-
aggregate(Sepal.Length ~ Species, iris, function(x) c(mean(x), median(x)))?或者只使用dplyr/data.table例如data.table(iris)[, .(mean(Sepal.Length), median(Sepal.Length)), by = Species]或iris %>% group_by(Species) %>% summarise(mean(Sepal.Length), median(Sepal.Length)) -
另一个
t(sapply(list(mean, median), function(x) with(iris, tapply(Sepal.Length, Species, x))))