Skip to content

Instantly share code, notes, and snippets.

@patternproject
Created September 27, 2016 03:55
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 patternproject/df1a5c7da6392c46622f8fff0a650345 to your computer and use it in GitHub Desktop.
Save patternproject/df1a5c7da6392c46622f8fff0a650345 to your computer and use it in GitHub Desktop.
How to save a list of data frames
# list of all data frames
s.df = names(which(sapply(.GlobalEnv, is.data.frame)))
# list of data frames with .p. in their names
s.df.p = s.df[str_detect(s.df,regex("\\.p\\.",ignore_case = FALSE))]
# creating path
paths <- paste0(s.df.p,".1", ".csv")
# helper function
#### write csv
save_df = function(df,f.name,flag=FALSE)
{
if(flag)
{
# removing initial x added to the col names
names(df) = gsub("x", "", names(df))
}
q.f.name = file.path("2. Output", f.name)
write.csv(x=df, file=q.f.name,row.names=FALSE)
}
# calling for magic
pwalk(list(s.df.p,paths, FALSE), save_df)
# I end up with csv files in right "paths" but containing only the ch names from the list s.df.p
@patternproject
Copy link
Author

The solution, kindly shared by @jennybryan
here: https://twitter.com/JennyBryan/status/780634488476798978

use purrr magic to save the csv

mget(s.df.p) %>% walk2(.,paths, read_csv)

where mget helps to extract the unerlying objects, instead of their characer names

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment