Created
March 30, 2014 19:03
-
-
Save jdmar3/9877894 to your computer and use it in GitHub Desktop.
Snippet to split dataframe into multiple dataframes using a sorting value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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