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)
.