Lets say I have a folder called "Muffins" which has a lot of different recipes for muffins (e.g., banana, blueberry, etc.), each in an independent file. I want to get all filenames within the "Muffins" folder.
My expected output is a data.frame of two columns: Column 1 = muffins (the folder name) and Column 2 = the individual file name.
Food Recipe
Muffins Banana
Muffins Blueberry
Muffins Chocolate
etc. etc.
I think this can be achieved with a simple for-loop, but I haven't find a way to make it work.
答案 0 :(得分:0)
I'm working on linux, so you may need to tweak this answer a little bit if you're in Windows:
As @Elin pointed out in the comments, list.files()
is your friend here. Please read the help file for it. We also will use the read.table
function in order to split the string returned by list.files
into the folder and file columns:
read.table(text = list.files("folder",
include.dirs= FALSE,
full.names=TRUE),
sep = "/")
What does the code do:
read.table
reads a vector and separates columns when it finds the "/".
list.files
searches "folder"
, returns full.names
, that is, file names that follow the folder name, as in a path, and does not include.dirs
.