Add margin row totals in dplyr chain

后端 未结 7 2128
一整个雨季
一整个雨季 2020-12-05 00:25

I would like to add overall summary rows while also calculating summaries by group using dplyr. I have found various questions asking how to do this, e.g. here, here, and he

相关标签:
7条回答
  • 2020-12-05 01:22

    maybe that works:

    library(dplyr)
    mtcars %>%
        # convert cyl column as.character
        mutate_at("cyl",as.character) %>%
        # add a copy of the origina data with cyl column = 'TOTAL'
        bind_rows(mutate(mtcars, cyl="total")) %>%
        group_by(cyl) %>% summarise_all(sum)
    
    0 讨论(0)
提交回复
热议问题