Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R summary function in ddply (plyr) in a simple way

Tags:

r

plyr

How would I replicate this with plyr?

with(mtcars, tapply(mpg, cyl, summary))

With the same elegance, i.e. without spelling out the individual statistics?

like image 924
Rico Avatar asked Jan 12 '23 18:01

Rico


1 Answers

library(plyr)
dlply(mtcars, .(cyl), function(x) summary(x$mpg))

You could also do this into a data.frame, which I generally prefer over complex lists...

ddply(mtcars, .(cyl), function(x) summary(x$mpg))

#   cyl Min. 1st Qu. Median  Mean 3rd Qu. Max.
# 1   4 21.4   22.80   26.0 26.66   30.40 33.9
# 2   6 17.8   18.65   19.7 19.74   21.00 21.4
# 3   8 10.4   14.40   15.2 15.10   16.25 19.2
like image 179
Justin Avatar answered Jan 16 '23 01:01

Justin