How to get summary statistics by group

前端 未结 10 1343
渐次进展
渐次进展 2020-11-22 05:13

I\'m trying to get multiple summary statistics in R/S-PLUS grouped by categorical column in one shot. I found couple of functions, but all of them do one statistic per call,

相关标签:
10条回答
  • 2020-11-22 05:37

    after 5 long years I'm sure not much attention is going to be received for this answer, But still to make all options complete, here is the one with data.table

    library(data.table)
    setDT(df)[ , list(mean_gr = mean(dt), sum_gr = sum(dt)) , by = .(group)]
    #   group mean_gr sum_gr
    #1:     A      61    244
    #2:     B      66    396
    #3:     C      68    408
    #4:     D      61    488 
    
    0 讨论(0)
  • 2020-11-22 05:38

    Besides describeBy, the doBy package is an another option. It provides much of the functionality of SAS PROC SUMMARY. Details: http://www.statmethods.net/stats/descriptives.html

    0 讨论(0)
  • 2020-11-22 05:39

    take a look at the plyr package. Specifically, ddply

    ddply(df, .(group), summarise, mean=mean(dt), sum=sum(dt))
    
    0 讨论(0)
  • 2020-11-22 05:41

    Using Hadley Wickham's purrr package this is quite simple. Use split to split the passed data_frame into groups, then use map to apply the summary function to each group.

    library(purrr)
    
    df %>% split(.$group) %>% map(summary)
    
    0 讨论(0)
提交回复
热议问题