Import all files (of a certain type) from the working directory and name them accordingly
Export all files from R to the working directory and name them accordingly
1) Importing
Suppose I have these files (of a certain type) on my working directory with different names(my_file.csv, my_r_file.csv, my_file.RDS, my_r_file.RDS etc.). The function below can list all of their names:
Suppose I have these files in R with different names - I would like to save all these files as csv files into the working directory. Suppose I have these files in R:
monday = data.frame(name = "john", day = "monday")
october = data.frame(name = "sara", month = "october")
# etc
I found this code that might be able to export (8) files provided they all start with "df":
dat <- do.call(rbind, lapply(1:8, function(x)get(paste0("df", x))))
#CSV Option
for(i in 1:ncol(dat)) write.csv(dat[,i], paste0("file_",i, ".csv"), row.names=F)
#RDS option
for(i in 1:ncol(dat)) saveRDS(dat[,i], paste0("file_",i, ".RDS"), row.names=F)
Are there more direct/efficient ways to bulk import/export files from/to R?
To import files, I like to use assign to construct the variable names. So to get at the CSVs, you could do something like this:
tmp_csv <- list.files(pattern = '*.csv')
lapply(
seq_along(tmp_csv),
function(i) {
# where i is the ith file in the file list
var_name <- paste('csv', i, sep = '_')
tmp_df <- read.csv(tmp_csv[i])
assign(var_name, value = tmp_df)
}
)
As far as exporting, I think your method is just fine.
Thank you so much! Just a question - can the code you have provided work if you replace "read.csv" with "write.csv"? Can list.files() also be used to get files from the Global R Environment? Thank you so much!
can the code you have provided work if you replace "read.csv" with "write.csv"?
If by this, you mean the body of the function inside lapply would look like this:
var_name <- paste('csv', i, sep = '_')
tmp_df <- write.csv(tmp_csv[i]) #instead of read.csv
assign(var_name, value = tmp_df)
This would not work, because the value of tmp_csv[i] is a string.
Regarding the environment - it's worthwhile to be a stickler about language here. "Files" cannot be stored in the .GlobalEnv (the global environment). Files are found in directories on your machine, whereas objects exist in environments. The reason it's important to be so particular about the language is because when you talk about files, it's not always clear if we mean:
The file name
The file data
The file metadata
Regarding working with the objects in your environment, @andresrcs 's answer is really excellent.