Search code examples
rdatabasedataframedirectorysubdirectory

How to make a data frame from different directories on R


I have set up my working directory which contains several folders with files in them. I would like to make a data frame with these files in each folder, however, the issue I am having is that using

list.files()

returns all of the names of the folders as strings (because it's looking at my working directory and just returning the names of the folders in them).

What I want to do is to get all of the files that is contained in each folder of the working directory and make a dataframe with all of it. Can anyone help? Let me know if my question doesn't make sense and I'd be happy to clarify!


Solution

  • Try

    list.files(recursive = T)
    

    It should display all files in all subdirectories, which I assume is what you want. Most functions in R have options like these, you can check them out in appropriate help files!

    Sorry, I don't get what you mean by 'make a dataframe with all of it'. If you want a data frame with all the file names, you can use

    as.data.frame(list.files(recursive = T))
    

    If you want to import all these files as data frames and merge them together, you could use a loop with read.csv() function inside, or something of the like. Hard to elaborate without knowing the specifics.

    Hope that was of help!