如何获得总计的汇总表

时间:2016-11-06 12:53:06

标签: r dplyr grouping totals

我想知道是否有更有效的方法来获得包含总计的汇总表。 我在这里做了四步程序。

data<-iris %>% group_by(Species) %>% 
  summarise(
    Sepal.Len = paste(format(round(median(Sepal.Length),2),nsmall=2) ), 
        P.len = paste(format(round(median(Petal.Length),2),nsmall=2) ) ,
            counts=n() )
datatotal<-iris %>% group_by(.) %>% 
  summarize(
    Sepal.Len = paste(format(round(median(Sepal.Length),2),nsmall=2) ), 
    P.len = paste(format(round(median(Petal.Length),2),nsmall=2) ) ,
    counts=n() )
datatotal<-cbind(Species="Total",datatotal)
final<-rbind(data,datatotal)
final
# A tibble: 4 × 4
     Species Sepal.Len P.len counts
*     <fctr>     <chr> <chr>  <int>
1     setosa      5.00  1.50     50
2 versicolor      5.90  4.35     50
3  virginica      6.50  5.55     50
4      Total      5.80  4.35    150

2 个答案:

答案 0 :(得分:2)

@ Richard的回答进一步改进,其中一切都在一个链中:

iris %>% 
  group_by(Species) %>%
  summarise(
    Sepal.Len = median(Sepal.Length),
    P.len = median(Petal.Length) ,
    counts = n()
  ) %>% 
  bind_rows(., iris  %>%
              summarize(
                Sepal.Len = median(Sepal.Length),
                P.len = median(Petal.Length) ,
                counts = n()
              ) %>%
              mutate(Species = "Total")
            ) %>% 
  mutate_each(funs(format(., nsmall = 2, digits = 2)), 2:3)

结果:

# A tibble: 4 × 4
     Species Sepal.Len P.len counts
       <chr>     <chr> <chr>  <int>
1     setosa      5.00  1.50     50
2 versicolor      5.90  4.35     50
3  virginica      6.50  5.55     50
4      Total      5.80  4.35    150

另一种方法是使用margins包中的dcast reshape2参数:

dcast(transform(melt(iris, id.vars = 'Species', measure.vars = c('Sepal.Length','Petal.Length')), 
                counts = ave(value, variable, Species, FUN = length)), 
      Species + counts ~ variable, 
      fun.aggregate = median,
      margins = 'Species')

结果(遗憾的是不完全如描述):

     Species counts Sepal.Length Petal.Length
1     setosa     50          5.0         1.50
2 versicolor     50          5.9         4.35
3  virginica     50          6.5         5.55
4      (all)  (all)          5.8         4.35

答案 1 :(得分:1)

您可以通过将格式设置移动到最终对象等来简化代码,但它不会让它更快更快

data <- iris %>% group_by(Species) %>%
  summarise(
    Sepal.Len = median(Sepal.Length),
    P.len = median(Petal.Length) ,
    counts = n()
  )

datatotal <- iris  %>%
  summarize(
    Sepal.Len = median(Sepal.Length),
    P.len = median(Petal.Length) ,
    counts = n()
  ) %>%
  mutate(Species = "Total")

final <- rbind(data, datatotal)
format(final, nsmall = 2, digits = 2)