本文介绍了如何迭代R脚本中的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 一个非常简单的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); 假设我有4个文件叫0.txt,1.txt,2.txt,...在同一个文件夹中,如何使用计数器(或别的东西)来遍历脚本中的文件名?解析方案 (0:3){ infile< - paste(i,。txt,sep =) outfile< - paste (i, - edit.txt,sep =) data colnames(data)[1] =time write.table(data,quote = FALSE,sep =,,outfile)} 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? 解决方案 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)} 这篇关于如何迭代R脚本中的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 13:46