0

I have a directory which contains different subfolders and other files. I need to access each subfolder, read the .tsv file and carry out the following rscript. How to loop this rscript and run it from the terminal?

for(i in my_files){
  s <- read.csv('abundance.tsv',sep = '\t')
  colnames(compare)[1] <- 'target_id'
  colnames(s)[1] <- 'target_id'
  s1 <- merge(compare, s, by = "target_id")
  output.filename <- gsub("(.*?)", "\\1.csv", i)
  write.table(s1, output.filename)
}
shome
  • 1,342
  • 1
  • 12
  • 30

1 Answers1

0

list.dirs() returns a list of the directories in the given path and list.files() a list of files in a given path, see here for the documentation.

list.dirs() can be recursive or not, so you can get only directory at the first level and then call list.dirs() again on each sub-directories (inside a loop) or directly get all the sub-directories.

With these two functions you can build your my_files array (since I do not know exactly your directory structure, I can't give an example).

If you have multiples files and want to open only some of them, you can check if the file name contains some sub-string you want (e.g. the file extension). The way to do it is shown here.

Chelmy88
  • 1,106
  • 1
  • 6
  • 17