Skip to content

Instantly share code, notes, and snippets.

@bfoste01
Created July 24, 2014 14:17
Show Gist options
  • Save bfoste01/77fe11b2e095b1f67a23 to your computer and use it in GitHub Desktop.
Save bfoste01/77fe11b2e095b1f67a23 to your computer and use it in GitHub Desktop.
#Fun stuff with plyr
#-------------------
#plyr .data, .variables to split on and .fun function applied
#understand the naming conventions of plyr with the first 2 letters
#e.g., ddply dd= split dataframe apply function out dataframe
#e.g., dlply dl = split dataframe apply function out list
#d = dataframe
#l = list
#a = array
#-------------------
#Groupwise Summaries
colnames(mtcars)
#[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"
library(plyr)
#highest mpg for cylinder type
ddply(mtcars, .(cyl), subset, mpg == max(mpg))
#selcect out the highest 25% of mpg per cylinder
ddply(mtcars, .(cyl), subset, mpg > quantile(mpg, .75))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment