【发布时间】:2016-10-18 05:56:15
【问题描述】:
可能是一个非常简单的答案,但我在循环中将一堆 .txt 文件转换为 .csv 文件时遇到了麻烦。我不想合并或附加它们,只需将 71 个单独的 .txt 文件转换为 71 个单独的 .csv 文件。
我正在尽我最大的努力用我的代码引用这个线程的其他问题: create a loop: convert .txt to .csv in R
我正在调整它,这就是我目前所拥有的:
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)
write.table(data, file=cur.output.file, sep=",", col.names=TRUE, row.names=FALSE)
}
但是,我的结果是:
[1] "Processing the file: filename1.txt.txt"
[1] "Processing the file: filename2.txt.txt"
[1] "Processing the file: filename3.txt.txt"
[1] "Processing the file: filename4.txt.txt"
[1] "Processing the file: filename5.txt.txt"
格式都搞砸了。
任何建议/我做错了什么?
干杯,谢谢。
【问题讨论】: