Home > front end >  How to loop first n folders and their subfolders to read the file in R?
How to loop first n folders and their subfolders to read the file in R?

Time:02-27

I have a folder with lets say 50 folders, and each of them contain some number of subfolders (only one depth), I need to loop through the first 10 folders, and then loop through every subfolders and read the single file that they contain. I tried to loop using list.dirs() but it gives all the subfolders as well.

CodePudding user response:

First, use top_folders = list.dirs(recursive = FALSE) to get the main folders. To get the 10 first, use e.g. head(top_folders, n = 10). Pass this to list.files(), with recursive = TRUE).

  •  Tags:  
  • r
  • Related