12

A very simple R script adds a ascending row index to a textfile "0.txt" and also adds a header "time" to it. After that the data is written to a file "0-edit.txt"

data<-read.table("0.txt", header=TRUE,sep=",",row.names= NULL);
colnames(data)[1] = "time"
write.table(data,quote=FALSE,sep=", ","0-edit.txt");

Assume i have 4 files called 0.txt, 1.txt, 2.txt, ...in the same folder, how can I use a counter (or something else) to iterate over those file names in my script?

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
Martin H
  • 749
  • 4
  • 10
  • 19

4 Answers4

15
for(i in 0:3) {
  infile <- paste(i,".txt",sep="")
  outfile <- paste(i,"-edit.txt",sep="")

  data <- read.table(infile,header=TRUE,sep=",",row.names=NULL)
  colnames(data)[1] = "time"
  write.table(data,quote=FALSE,sep=", ",outfile)
}
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
15

Here is a solution without a loop using lapply:

infiles <- dir(pattern='\\.txt$')

change.files <- function(file){
  data <- read.table(file, header=TRUE, sep=",", row.names=NULL)
  colnames(data)[1] = "time"
  write.table(data, quote=FALSE, sep=", ", sub("\\.txt$","-edit.txt", file))
}

lapply(infiles , change.files)

For me the real advantage of this approach is that you can easily run it in parallel using mclapply (from multicore package) instead of lapply. Or parLapply from snow. Also to me it looks nicer.

Matti Pastell
  • 9,135
  • 3
  • 37
  • 44
7

Try this:

files <- list.files(path="", pattern=".txt", all.files=T, full.names=T)
for (file in files) {
## do stuff
}

You can use regular expressions for the pattern matching, so if you have lots of text files but only want the ones with numeric names, use "[0-9].txt" or "[0-3].txt".

Mason D
  • 71
  • 1
3

More generally, you can use dir() to get the files in a given directory, and use select to limit it to .txt files.

file.dir <- "/path/to/files"
for(infile in dir(file.dir, pattern="\\.txt$")) {
  outfile <- gsub("\\.txt$","-edit\\.txt", infile)

  data <- read.table(infile,header=TRUE,sep=",",row.names=NULL)
  colnames(data)[1] = "time"
  write.table(data,quote=FALSE,sep=", ",outfile)
}
alaiacano
  • 693
  • 1
  • 5
  • 10