Using dplyr to summarize by multiple groups

后端 未结 2 1518
一向
一向 2021-01-21 16:44

I\'m trying to use dplyr to summarize a dataset based on 2 groups: \"year\" and \"area\". This is how the dataset looks like:

  Year   Area Num
1 2000 Area 1  99         


        
相关标签:
2条回答
  • We can use data.table

    library(data.table)
    setDT(df)[, .(avg = mean(Num)) , by = .(Year, Area)]
    
    0 讨论(0)
  • 2021-01-21 17:12

    Is this what you are looking for?

     library(dplyr)
     df <- group_by(df, Year, Area)
     df <- summarise(df, avg = mean(Num))
    
    0 讨论(0)
提交回复
热议问题