Need to write a piece of code in R that'll create a list specifying:
I've tried to use the following generic function but had no luck:
list.files(path, pattern=NULL, all.files=FALSE,
full.names=FALSE)
dir(path, pattern=NULL, all.files=FALSE,
full.names=FALSE)
Would very much appreciate your response.
I think what your are missing is the recursive = TRUE
parameter in list.files()
One possible solution could be to list all files first and then limit the output to 2 levels accordingly.
files <- list.files(path = "D:/cmder/", recursive = TRUE)
Since R represents paths using "/" a simple example could be to remove everything that has more than 3 slashes if you need a depth of 2.
files[!grepl(".*/.*/.*/.*", files)]
Be careful on windows as you might see a back slash "\" there sometimes, only if your path information comes from something different that R itself, e.g. a csv import.
My grepl()
statement can probably be improved as I'm not an expert there.