variable in the file name for write.table in R
Please help me with naive question (already googled, and tried a lot of variations, but failed): How save files with the variable in the file name for write.table in R? Script loop over the files in dir, apply some functions and then save results into the file with the same name but additional ending. Thank's!
for (x in list.files(pattern="SIM")) {
u <- read.t开发者_如何学Pythonable(x, header = T, row.names = 1, sep = " ")
ut <- t(u)
utm <- colMeans(ut)
utms <- sort(utm, decreasing = T)
write.table(utms, "$x.mean")
}
You can use paste
to do this.
Try the following:
write.table(utms, file=paste(x, ".mean", sep=""))
paste
concatenates character vectors. See ?paste
for more details.
The sprintf function can also be used for this type of thing with a little different syntax:
write.table(utms, file=sprintf("%s.mean",x))
You can use also the following for data frames:
now<-format(Sys.time(), "%d-%m-%Y")
csvFileName <- paste("Your_df_",now,".csv",sep="")
write.csv(WKSH_Check, file=csvFileName)
精彩评论