Is there a quick way on Mac to put thousands of files from different folders into one folder or is there a way for R to read thousands of files in different working directories simultaneously?
Asked
Active
Viewed 589 times
-1
-
What do you mean by different directories? Are these directories sub directory of one parent directory? Else what's the criteria to identify these directories? – AnilGoyal Jan 02 '21 at 06:39
-
for example, I have a folder A, which includes 3 subfolders b1, b2, b3, and each of the subfolders contains .rds files I need to read into R. – Rlearner Jan 02 '21 at 09:57
1 Answers
1
Some relevant details are missing from your question (e.g. file format, whether all the subfolders are in the same main folder). However, you may wanna look over two functions:
list.files()
combined to
lapply()
When reading the files are a good place to start. Please provide more details so we can help you in a better way.

CRP
- 405
- 2
- 11
-
sorry about the incomplete information, for example, I have a folder A, which includes 3 subfolders b1, b2, b3, and each of the subfolders contains .rds files I need to read into R. – Rlearner Jan 02 '21 at 09:58
-
Oh thanks for clarifying. If your workspace is set to A, you could try the following: files<-list.files(path = '.',pattern='rds', recursive = T, full.names = T) rds_files <- lapply(files, readRDS) You can also do purr :) library(purrr) rds_files<-purrr::map_df(files, readRDS) – CRP Jan 02 '21 at 15:32
-
Thanks! Another question, what is the order of the elements in the rds_files, there are no names for each element in it, so it is hard to know which element is which file in the original folders – Rlearner Jan 02 '21 at 17:45
-
Here's some additional information: https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/list.files. You can also do ?list.files in R to retrieve the same information. Note that the full.names = T argument should give you the full name for each of the files – CRP Jan 02 '21 at 20:20