R aggregate by large number of columns

后端 未结 4 606
青春惊慌失措
青春惊慌失措 2020-12-20 08:04

I have a data frame (df) that has about 40 columns, and I want to aggregate using a sum on 4 of the columns. Outside of the 4 I want to sum, each unique value in column 1 co

4条回答
  •  春和景丽
    2020-12-20 08:17

    This would be the current answer with dplyr:

    library('dplyr')
    mytb<-read.table(text="
    A B C D Sum
    1 A B C D   1
    2 A B C D   2
    3 A B C D   3
    4 E F 1 R   4
    5 E F 1 R   5", header=T, stringsAsFactors=F)
    
    mytb %>% 
      group_by_at(names(select(mytb, -"Sum") ) )  %>% 
      summarise_all(.funs=sum)
    

提交回复
热议问题