使用总行创建表格摘要

时间:2012-07-13 21:38:46

标签: r aggregate

是否有优雅的单行(使用任何R包)来完成以下操作?

tab <- aggregate(. ~ Species, dat=iris, mean)
total <- data.frame(Species='Overall', t(colMeans(iris[,-5])))
rbind(tab, total)

2 个答案:

答案 0 :(得分:6)

套餐tables

library(tables)
tabular( (Species + 1) ~ All(iris)*(mean),data=iris)
> tabular( (Species + 1) ~ All(iris)*(mean),data=iris)

            Sepal.Length Sepal.Width Petal.Length Petal.Width
 Species    mean         mean        mean         mean       
 setosa     5.006        3.428       1.462        0.246      
 versicolor 5.936        2.770       4.260        1.326      
 virginica  6.588        2.974       5.552        2.026      
 All        5.843        3.057       3.758        1.199  

但我作弊并对帮助文件中的示例进行了轻微复制;)因此归功于Duncan Murdoch。

sqldf

library(sqldf)

库(sqldf)

sqldf(" 
   select Species, 
       avg(Sepal_Length) `Sepal.Length`,
       avg(Sepal_Width) `Sepal.Width`,
       avg(Petal_Length) `Petal.Length`,
       avg(Petal_Width) `Petal.Width` 
    from iris 
    group by Species 
union all 
    select 'All', 
       avg(Sepal_Length) `Sepal.Length`,
       avg(Sepal_Width) `Sepal.Width`, 
       avg(Petal_Length) `Petal.Length`,
       avg(Petal_Width) `Petal.Width` 
    from iris"
)

可以像这样写得更紧凑:

variables <- "avg(Sepal_Length) `Sepal.Length`,
    avg(Sepal_Width) `Sepal.Width`,
    avg(Petal_Length) `Petal.Length`,
    avg(Petal_Width) `Petal.Width`"

fn$sqldf(" select Species, $variables from iris group by Species 
    union all select 'All', $variables from iris")

     Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1     setosa     5.006000    3.428000        1.462    0.246000
2 versicolor     5.936000    2.770000        4.260    1.326000
3  virginica     6.588000    2.974000        5.552    2.026000
4        All     5.843333    3.057333        3.758    1.199333

答案 1 :(得分:3)

reshape2在这里可能有点流畅,分为两步:

library(reshape2)
iris.m <- melt(iris, id.vars = "Species")
dcast(Species ~ variable, data = iris.m, fun.aggregate = mean, margins = "Species")
#-----
     Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1     setosa     5.006000    3.428000        1.462    0.246000
2 versicolor     5.936000    2.770000        4.260    1.326000
3  virginica     6.588000    2.974000        5.552    2.026000
4      (all)     5.843333    3.057333        3.758    1.199333

请参阅有关?dcast的边距参数的详细信息