请帮我解决一个幼稚的问题(已经用 google 搜索过,并尝试了很多变体,但都失败了):How save files with the variable in the file name for write.table in R?脚本循环遍历 dir 中的文件,应用一些函数,然后将结果保存到具有相同名称但附加结尾的文件中。谢谢!
for (x in list.files(pattern="SIM")) {
u <- read.table(x, header = T, row.names = 1, sep = " ")
ut <- t(u)
utm <- colMeans(ut)
utms <- sort(utm, decreasing = T)
write.table(utms, "$x.mean")
}
最佳答案
您可以使用 paste
去做这个。
请尝试以下操作:
write.table(utms, file=paste(x, ".mean", sep=""))
paste
连接字符向量。见 ?paste
更多细节。
关于r - R 中 write.table 的文件名中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6773342/