how to compute rowsums using tidyverse

前端 未结 4 1680
耶瑟儿~
耶瑟儿~ 2020-12-30 14:23

I did mtcars %>% by_row(sum) but got the message:

by_row() is deprecated; please use a combination of: tidyr::nest(); d

4条回答
  •  伪装坚强ぢ
    2020-12-30 14:50

    Furthermore, you can use conditional subsetting, but then you sum up the number of the columns that meet the criterion, not the values:

    mtcars %>%
        select(all_of(c('gear', 'carb'))) %>%
        mutate(
            high_gear_carb = rowSums(. > 3)
        )
    
       gear carb high_gear_carb
    1     4    4              2
    2     4    4              2
    3     4    1              1
    4     3    1              0
    5     3    2              0
    6     3    1              0
    7     3    4              1
    ...
    

提交回复
热议问题