Using dplyr summarize with different operations for multiple columns

前端 未结 5 936
太阳男子
太阳男子 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:48

    Fortunately, there is a much simpler way available now. With the new dplyr 1.0.0 coming out soon, you can leverage the across function for this purpose.

    All you need to type is:

    iris %>% 
      group_by(Species) %>% 
      summarize(
        # I want the sum over the first two columns, 
        across(c(1,2), sum),
        #  the mean over the third 
        across(3, mean),
        # the first value for all remaining columns (after a group_by(Species))
        across(-c(1:3), first)
      )
    

    Great, isn't it? I first thought the across is not necessary as the scoped variants worked just fine, but this use case is exactly why the across function can be very beneficial.

    You can get the latest version of dplyr by devtools::install_github("tidyverse/dplyr")

提交回复
热议问题