How to create nested list from a nested directory of csv files?

Hi,

R newbie here. I have a main folder (directory) that contains a few levels of subfolders before reaching my csv files. Ideally, I'd like to read this data in and save it as a nested list in R, where each level of the list corresponds to the level of subfolder.

For example, suppose my main folder has 2 subfolders: A and B. A has 3 subfolders of its own: a,b,c, while B has subfolders: d,e,f. The folders a,b,c,d,e, and f each contains their own csv files.

Any idea how to read this kind of data in? Normally, I use list.files() if I have a single folder with files, but I can't seem to get it working for the nested folder structure.

Thank you!!

Hi there,

You can do this easily by setting the recursive option in the list.files function to TRUE like this:

list.files(".", ".csv", recursive = T)

Hope this helps,
PJ

I may be wrong, but I think "nested list" is the main question. list.files returns all paths as a vector.

This solves the question I believe:

1 Like