简体   繁体   English

使用Lapply保存文件时如何添加字符

[英]How to add a character when saving files with lapply

I have the following list 我有以下清单

  L = list.files(".", ".txt")

which is 这是

   a.txt
   b.txt
   c.txt

and I want to apply some code to all files in that list, but I want to save the dataframes with the samename plus some character to indicate that it is modified. 我想对该列表中的所有文件应用一些代码,但是我想保存具有相同名称和一些字符的数据框,以表明它已被修改。 For example 例如

   a_modified.txt
   b_modified.txt
   c_modified.txt

I'm currently used this code: 我目前正在使用此代码:

   datalist = lapply(L, function(x) {
   DF = read.csv(x, sep = ",")
   DF$X = gsub("[:.:][[:digit:]]{1,3}","", DF$X))
   colnames(DF)[colnames(DF)=="X"] <- "ID"
   DF <- merge(DF, genes ,by="ID")
   write.csv(DF, x)
   return(DF)
   })

I tried using 我尝试使用

   write.csv(DF, x+"_modified")

which was obviously wrong, as write.csv does not accept this exact operation. 这显然是错误的,因为write.csv不接受此精确操作。

Any ideas? 有任何想法吗?

We need paste instead of + 我们需要paste而不是+

write.csv(DF, paste0(sub("\\.txt", "", x), "_modified.csv"))

or this can be done within sub itself 或者这可以在sub本身内完成

write.csv(DF, sub("\\.txt", "_modified.csv", x))

NOTE: intial datasets were .txt 注意:初始数据集为.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM