Skip to content

Instantly share code, notes, and snippets.

@glamp
Last active December 12, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glamp/4750641 to your computer and use it in GitHub Desktop.
Save glamp/4750641 to your computer and use it in GitHub Desktop.
library(plyr)
# split a data frame by Species, summarize it, then convert the results
# into a data frame
ddply(iris, .(Species), summarise,
mean_petal_length=mean(Petal.Length)
)
# Species mean_petal_length
#1 setosa 1.462
#2 versicolor 4.260
#3 virginica 5.552
# split a data frame by Species, summarize it, then convert the results
# into an array
unlist(daply(iris[,4:5], .(Species), colwise(mean)))
# setosa.Petal.Width versicolor.Petal.Width virginica.Petal.Width
# 0.246 1.326 2.026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment