Collapsing a data frame over one variable

后端 未结 3 1066
[愿得一人]
[愿得一人] 2021-01-27 14:26

I have a data frame in the following format:

Site    Year    Month   Count1  Count2  Count3  Patch
1        1        May     15      12      10      1
1        1         


        
3条回答
  •  臣服心动
    2021-01-27 15:11

    library(dplyr) 
    dat %>% 
    group_by(Site, Year, Month) %>% 
    summarise_each(funs(sum=sum(., na.rm=TRUE)), Count1:Count3)
    # Source: local data frame [3 x 6]
    #Groups: Site, Year
    
    #    Site Year Month Count1 Count2 Count3
    #  1    1    1  July      4      0      3  
    #  2    1    1  June     23     11      6
    #  3    1    1   May     30     17     18
    

提交回复
热议问题