Using dplyr summarize with different operations for multiple columns

前端 未结 5 934
太阳男子
太阳男子 2020-12-17 01:20

Well, I know that there are already tons of related questions, but none gave an answer to my particular need.

I want to use dplyr \"summarize\" on a table with 50 co

5条回答
  •  攒了一身酷
    2020-12-17 01:47

    You could summarise the data with each function separately and then join the data later if needed.

    So something like this for the iris example:

    sums <- iris %>% group_by(Species) %>% summarise_at(1:2, sum)
    means <- iris %>% group_by(Species) %>% summarise_at(3, mean)
    firsts <- iris %>% group_by(Species) %>% summarise_at(4, first)
    full_join(sums, means) %>% full_join(firsts)
    

    Though I would try to think of something else if there are more than a handful of summarising functions you need to use.

提交回复
热议问题