How to get summary statistics by group

前端 未结 10 1351
渐次进展
渐次进展 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:20

    I'll put in my two cents for tapply().

    tapply(df$dt, df$group, summary)
    

    You could write a custom function with the specific statistics you want to replace summary.

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

    dplyr package could be nice alternative to this problem:

    library(dplyr)
    
    df %>% 
      group_by(group) %>% 
      summarize(mean = mean(dt),
                sum = sum(dt))
    

    To get 1st quadrant and 3rd quadrant

    df %>% 
      group_by(group) %>% 
      summarize(q1 = quantile(dt, 0.25),
                q3 = quantile(dt, 0.75))
    
    0 讨论(0)
  • 2020-11-22 05:31

    The psych package has a great option for grouped summary stats:

    library(psych)
        
    describeBy(dt, group="grp")
    

    produces lots of useful stats including mean, median, range, sd, se.

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

    First, it depends on your version of R. If you've passed 2.11, you can use aggreggate with multiple results functions(summary, by instance, or your own function). If not, you can use the answer made by Justin.

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

    There's many different ways to go about this, but I'm partial to describeBy in the psych package:

    describeBy(df$dt, df$group, mat = TRUE) 
    
    0 讨论(0)
  • 2020-11-22 05:35

    While some of the other approaches work, this is pretty close to what you were doing and only uses base r. If you know the aggregate command this may be more intuitive.

    with( df , aggregate( dt , by=list(group) , FUN=summary)  )
    
    0 讨论(0)
提交回复
热议问题