0

I am trying to learn how to:

  • 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:

temp_RDS = list.files(pattern="*.RDS")
temp_CSV = list.files(pattern="*.csv")

But from here, I don't know how to bulk import them and name them accordingly (using an index). For example:

#automate

file_1 = read.csv("my_file.csv")
file_2 = read.csv("my_r_file.csv")

file_1 = read.csv("my_file.RDS")
file_2 = read.csv("my_r_file.RDS")

2) Exporting

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" - but this will not work if all the files are named differently:

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?

Thank you!

stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • Have you seen this? https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once and https://stackoverflow.com/questions/31296398/r-save-all-data-frames-in-workspace-to-separate-rdata-files – Skaqqs Jun 14 '22 at 17:38
  • @ Skaqqs : Yes, I consulted several similar posts and got some of the code I used from there! – stats_noob Jun 14 '22 at 17:40
  • So for your question 1, did this work? `temp = list.files(pattern="*.csv"); myfiles = lapply(temp, read.delim)`? – Skaqqs Jun 14 '22 at 17:44
  • And for question 2, `dfs<-Filter(function(x) is.data.frame(get(x)) , ls()); for(d in dfs) {write.csv(d, file=paste0(d, ".csv"))}`? – Skaqqs Jun 14 '22 at 17:45
  • @ Skaqqs : Thank you for your reply! For question 1 - "myfiles" has been created as a large list. How do I break "myfiles" into smaller files that are named "file_1", "file_2", etc.? – stats_noob Jun 14 '22 at 21:09
  • Question 2 seems to be working fine! Thank you so much! – stats_noob Jun 14 '22 at 21:11
  • 1
    It's usually not recommended, but you could use assign(), e.g., https://stackoverflow.com/a/39675290 – Skaqqs Jun 15 '22 at 02:25
  • @ Skaqqs: Thank you for your reply! Why is "assign()" not usually recommended? If you have time, could you please show me how to import files and name them accordingly (e.g. file_1, file_2...file_i)? Thank you so much! – stats_noob Jun 15 '22 at 03:09
  • @ Skaqqs: I think I solved my own question! https://stackoverflow.com/questions/72625690/importing-files-using-list-files – stats_noob Jun 15 '22 at 03:48
  • Ok, great! I'm glad you solved your question. If you feel that this was a novel question, you could post your own solution as an answer and mark it as correct. Or if you feel that your question was sufficiently answered by the linked questions, you could mark this question as a duplicate/delete. Best of luck! – Skaqqs Jun 15 '22 at 12:46

0 Answers0