-2

I am on a machine running Windows 10 with RStudio (0.99.893). I am running into a problem whereby I want to include ALL filetypes (.cnv) into one dataframe. In the past all the files have been in one directory so this following has worked fine:

setwd(directory path)
df <- c()
for (x in list.files(pattern="*.cnv")) {
u<-read.table(x)
      u$Filename = factor(x)
      df <- rbind(df, u)
}

Now I am faced with a situation where there are several sub-directories with irregular names. Before I was telling R "move to this directory, look for all files with .cnv, then combine them into one dataframe". Now I need to tell R "move to this directory, look in this directory and all sub-directories for files with .cnv then combine them into a dataframe.

Any idea how I might accomplish within R?

boshek
  • 4,100
  • 1
  • 31
  • 55

1 Answers1

1

Set recursive=TRUE should work:

setwd(directory path)
df <- c()
for (x in list.files(pattern="*.cnv", recursive=TRUE)) {
      u<-read.table(x)
      u$Filename = factor(x)
      df <- rbind(df, u)
}

From ?list.files:

recursive: logical. Should the listing recurse into directories?

989
  • 12,579
  • 5
  • 31
  • 53