'how to efficiently build a summary table

I have small df, and I want to get a summary table of the score for all, enroll=1, enroll=0. how can I do it at once.

I want sth that looks like following (the summary is not from my data): enter image description here

df<-structure(list(score = c(1022.77358490566, 2508.80952380952, 
844.973684210526, 1650.84146341463, 2336.84210526316, 965.775, 
1287, 479.368421052632, 407.060606060606, 2850, 611.228070175439, 
959.704142011834, 1107.82258064516, 310.684684684685, 325.834951456311
), enroll = c(0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0)), row.names = c(NA, 
-15L), class = c("tbl_df", "tbl", "data.frame"))
r


Solution 1:[1]

Something like this?

Using data.table

library(data.table)
setDT(df)     # convert to data.table
rbind(
  df[, as.list(summary(score)), by=.(enroll)],
  df[, c(enroll='Overall', as.list(summary(score)))]
  )
##     enroll     Min.  1st Qu.    Median     Mean   3rd Qu.     Max.
## 1:       0 325.8350 386.7542  686.4178  680.361  980.0246 1022.774
## 2:       1 310.6847 728.1009 1107.8226 1358.843 1993.8418 2850.000
## 3: Overall 310.6847 545.2982  965.7750 1177.915 1468.9207 2850.000

Solution 2:[2]

you can use tapply

tapply(df$score, df$enroll, summary)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jlhoward
Solution 2 Mel G