使用新名称在fread后写入文件

时间:2016-04-04 08:59:02

标签: r data.table lapply fread

假设我有这样的文件,

cat("TRIAL\nTRIAL\nblah\nVARIABLE,X1,X2\nA,1,2\n", file="myfile1.csv")
cat("blah\nVARIABLE\nTRIAL,A1,A2\nA,1,2\n", file="myfile2.csv")
cat("blah\nblah\nVARIABLE\nTRIAL,Z1,Z2\nA,1,2\n", file="myfile3.csv")

flist<-list.files(pattern = "myfile.*.csv")

[1] "myfile1.csv" "myfile2.csv" "myfile3.csv"

我用fread!grep一些字符​​串阅读这些文件&#34; TRIAL&#34;

result <- lapply(flist,function(x)fread(paste("grep -v TRIAL",x),header = FALSE)[,FNAME:=x])

然后如果我尝试编写这样的文件"myfile1_new.csv" "myfile2_new.csv" "myfile3_new.csv" ;

lapply(result,function(x) write.table(x,file = gsub("(.*)(\\..*)", "\\1_new\\2", flist),row.names = FALSE,col.names = TRUE))
  

文件错误(文件,ifelse(追加,&#34; a&#34;,&#34; w&#34;)):无效   &#39;描述&#39;参数另外:警告消息:在if(file ==   &#34;&#34;)file&lt; - stdout()else if(is.character(file)){:条件   长度> 1,只使用第一个元素

我知道result是一个列表

str(result) 3个列表,但我收到的错误与上述类似。

此外,如何将freadwrite.table合并到同一个lapply循环中?

问候

2 个答案:

答案 0 :(得分:1)

第一部分:

gsub("(.*)(\\..*)", "\\1_new\\2", flist)会返回一个列表,因此您需要将其中的元素传递到write.table,我会在seq_along()函数中使用lapply()执行此操作,如下所示:

cat("TRIAL\nTRIAL\nblah\nVARIABLE,X1,X2\nA,1,2\n", file = "myfile1.csv")
cat("blah\nVARIABLE\nTRIAL,A1,A2\nA,1,2\n", file = "myfile2.csv")
cat("blah\nblah\nVARIABLE\nTRIAL,Z1,Z2\nA,1,2\n", file = "myfile3.csv")

flist <- list.files(pattern = "myfile.*.csv")

result <-
  lapply(flist, function(x)
    fread(x, header = FALSE)[, FNAME := x])

# str(gsub("(.*)(\\..*)", "\\1_new\\2", flist))  # returns a list

lapply(seq_along(result), function(i) {
  # print(gsub("(.*)(\\..*)", "\\1_new\\2", flist))  # a list
  # print(gsub("(.*)(\\..*)", "\\1_new\\2", flist)[i])  # this is what you want
  write.table(
    result[i],
    file = gsub("(.*)(\\..*)", "\\1_new\\2", flist[i]),
    row.names = FALSE,
    col.names = TRUE
  )
})

第二部分(合并为一个lapply())可能涉及来自%>%包的管道(dplyr)运算符。

编辑:第二部分:

library(dplyr)

lapply(
  seq_along(flist),
  FUN = function(i) {
    # fread(paste("grep -v TRIAL", x), header = FALSE)[, FNAME := x])
    fread(flist[i], header = FALSE)[, FNAME := flist[i]] %>%
      write.table(
        file = gsub("(.*)(\\..*)", "\\1_new\\2", flist[i]),
        row.names = FALSE,
        col.names = TRUE
      )
  }
)

或者您可以将fread()的结果分配给临时变量,然后执行write.table(temp, file = ...)

答案 1 :(得分:1)

for (filename in flist) {
  d = fread(paste("grep -v TRIAL", filename), header = FALSE)[, FNAME := filename]
  write.table(d, gsub("(.*)(\\..*)", "\\1_new\\2", filename),
              row.names = FALSE, col.names = TRUE)
}