43

I need to apply a set of commands in R to all the individual .txt files (around 300) in a directory.

I'm not very familiar with R, so all the help I've looked at online about looping is confusing, or I can't work out how to apply a loop when you need to apply multiple commands to each file.

The commands I need to apply to each file (phylogenetic trees) within the directory are (which uses R's ape library):

testtree <- read.tree("tree123.txt")
unrooted_tr <- unroot(testtree)
write.tree(unrooted_tr, file="unrootedtree123.txt")

How do I apply a loop which will apply these commands to each individual .txt file (either using R or in the Unix command line)? The output (e.g. unrootedtree123.txt) will need to have a different name for each individual file.

Thanks in advance, Dani.

Henrik
  • 65,555
  • 14
  • 143
  • 159
user2087201
  • 431
  • 1
  • 5
  • 3
  • Do you have vector of the filenames or do the files follow some kind of naming convention (e.g. tree[3digitnumber])? – sebastian-c Feb 19 '13 at 13:27

1 Answers1

92

You can get all the files and then loop using lapply and apply whatever function you want to apply as follows:

files <- list.files(path="path/to/dir", pattern="*.txt", full.names=TRUE, recursive=FALSE)
lapply(files, function(x) {
    t <- read.table(x, header=TRUE) # load file
    # apply function
    out <- function(t)
    # write to file
    write.table(out, "path/to/output", sep="\t", quote=FALSE, row.names=FALSE, col.names=TRUE)
})
Aren Cambre
  • 6,540
  • 9
  • 30
  • 36
Arun
  • 116,683
  • 26
  • 284
  • 387
  • This does not work. Looping through a directory of csvs gives this error: `Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : line 1 did not have 2 elements` –  Jun 18 '20 at 20:48