Created
September 27, 2016 03:55
-
-
Save patternproject/df1a5c7da6392c46622f8fff0a650345 to your computer and use it in GitHub Desktop.
How to save a list of data frames
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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