如何在R脚本中迭代文件名?

时间:2022-03-18 18:17:57

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"

一个非常简单的R脚本将升序行索引添加到文本文件“0.txt”,并为其添加标题“time”。之后,数据被写入文件“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?

假设我在同一文件夹中有4个名为0.txt,1.txt,2.txt,...的文件,我如何使用计数器(或其他东西)在我的脚本中迭代这些文件名?

4 个解决方案

#1


13  

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)
}

#2


12  

Here is a solution without a loop using lapply:

这是一个没有使用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.

对我来说,这种方法的真正优势在于您可以使用mclapply(来自多核包)而不是lapply轻松地并行运行它。或者来自雪地的parLapply。对我来说它看起来更好。

#3


6  

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".

您可以使用正则表达式进行模式匹配,因此如果您有许多文本文件但只想要具有数字名称的文本文件,请使用“[0-9] .txt”或“[0-3] .txt”。

#4


3  

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

更一般地,您可以使用dir()来获取给定目录中的文件,并使用select将其限制为.txt文件。

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)
}

#1


13  

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)
}

#2


12  

Here is a solution without a loop using lapply:

这是一个没有使用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.

对我来说,这种方法的真正优势在于您可以使用mclapply(来自多核包)而不是lapply轻松地并行运行它。或者来自雪地的parLapply。对我来说它看起来更好。

#3


6  

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".

您可以使用正则表达式进行模式匹配,因此如果您有许多文本文件但只想要具有数字名称的文本文件,请使用“[0-9] .txt”或“[0-3] .txt”。

#4


3  

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

更一般地,您可以使用dir()来获取给定目录中的文件,并使用select将其限制为.txt文件。

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)
}