Skip to content

Instantly share code, notes, and snippets.

@jdmar3
Created March 30, 2014 19:03
Show Gist options
  • Save jdmar3/9877894 to your computer and use it in GitHub Desktop.
Save jdmar3/9877894 to your computer and use it in GitHub Desktop.
Snippet to split dataframe into multiple dataframes using a sorting value
# Split multi-year dataframe into smaller dataframes by year.
# This will label the new dataframes as "year1997", "year1998", etc.
# Replace DATAFRAME below with the name of your dataframe object
invisible(lapply(split(DATAFRAME, DATAFRAME$year), function(x) {
assign(paste0("year", x$year[1]), x, pos=.GlobalEnv) }))
# You can change the sort value to country or whatever other variable
# you like by changing all instances of "year" to whatever the other
# variable is, e.g. "country" or "wbnum", etc. Note that this will
# create a number of dataframes equal to whatever the number of levels
# for the chosen sorting variable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment