Add margin row totals in dplyr chain

后端 未结 7 2126
一整个雨季
一整个雨季 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)
    

提交回复
热议问题