创建一个循环:将.txt转换为R

时间:2016-02-06 07:09:49

标签: r csv read.table

我尝试转换.csv中的所有.txt文件,但我没有设法创建循环。 一个文件的实际行(完美地工作)将如下:

tab = read.delim("name_file", header = TRUE, skip = 11)
write.table(tab, file="name_file.csv",sep=",",col.names=TRUE,row.names=FALSE)

我想为wd中的所有.txt文件执行此操作。

我根据网络上的一些研究尝试了循环,但我不确定它是否正确:

FILES = list.files(pattern = ".txt")
for (i in 1:length(FILES)) {
  FILES = read.csv(file = FILES[i], header = TRUE, skip = 11, fill = TRUE)
  write.csv(FILES, file = paste0(sub("folder_name", ".txt","", FILES[i]), ".csv"))
}

我在Windows系统上。 我很感激一些帮助...谢谢!

3 个答案:

答案 0 :(得分:1)

嗨,我和你一样有同样的问题,现在我做到了。试试这个:

directory <- "put_your_txt_directory_here"
ndirectory <- "put_your_csv_directory_here"

file_name <- list.files(directory, pattern = ".txt")

files.to.read <- paste(directory, file_name, sep="/") 
files.to.write <- paste(ndirectory, paste0(sub(".txt","", file_name),".csv"), sep="/")

for (i in 1:length(files.to.read)) {
  temp <- (read.csv(files.to.read[i], header = TRUE, skip = 11, fill = TRUE))
  write.csv(temp, file = files.to.write[i])
}

答案 1 :(得分:0)

您还需要在循环内索引输出。试试这个:

INFILES = list.files(pattern = ".txt")

OUTFILES = vector(mode = "character", length = length(INFILES))

for (i in 1:length(INFILES)) {
   OUTFILES[i] = read.csv(file = INFILES[i], header = TRUE, skip = 11, 
                    fill = TRUE)
   write.csv(OUTFILES[i], file = paste0("folder_name", sub(".txt","", INFILES[i]), ".csv"))
 }

答案 2 :(得分:0)

假设你的输入文件总是至少有11行(因为你跳过了前11行!)这应该有效:

filelist = list.files(pattern = ".txt")

for (i in 1:length(filelist)) {
  cur.input.file <- filelist[i]
  cur.output.file <- paste0(cur.input.file, ".csv") 
  print(paste("Processing the file:", cur.input.file))

  # If the input file has less than 11 rows you will reveive the error message:
  # "Error in read.table: no lines available in input")
  data = read.delim(cur.input.file, header = TRUE, skip = 11)
  write.table(data, file=cur.output.file, sep=",", col.names=TRUE, row.names=FALSE)
}

如果您在文件转换期间发现任何错误,则由内容引起(例如,每列的行数不等,列数不等等)。

PS:在这里使用for循环是可以的,因为它不会限制性能(没有&#34;矢量化&#34;逻辑来读写文件)。