write.table不会附加来自提取的不同csv文件的信息

时间:2019-02-19 14:26:32

标签: r append write.table

我想从设置的csv文件中提取一些值,并将它们附加到新的csv文件中。 当我使用write.table(append = T)时,信息不会被附加但会被覆盖。

for(Tsd in c(0.001))
{
  for(Fsd in c(2,5,10,15,20,30,60))
  {
    nameoffile <- paste("summary_out_T_",Tsd,"_F_",Fsd,"_2.csv",sep="")
    durdata <- read.csv(file = nameoffile, header=TRUE, sep=",")

    name2 <- paste("duration.csv", sep = "")
    file.create(name2)

    a=c("Fsd", "onSD")
    write.table(as.matrix(t(a)), sep = ",", name2, col.names = F, row.names = F, append = T)

    aa = c(Fsd, durdata[[16,14]])
    write.table(as.matrix(t(aa)), sep = ",", name2, col.names = F, row.names = F, append = T)
  }
}

输出文件“ duration.csv” 应该有8行

> Fsd onSD 
> 2   a
> 5   b
> 10  c
> 15  d
> 20  e    
> 30  f
> 60  g

相反,它只有

> Fsd onSD
> 60  g

有什么我想念的吗?不应该附加= T来解决这个问题?

1 个答案:

答案 0 :(得分:0)

问题在这里:file.create(name2):您正在重新创建文件并覆盖。 尝试

name2 <- paste("duration.csv", sep = "")
file.create(name2)


for(Tsd in c(0.001))
{
  for(Fsd in c(2,5,10,15,20,30,60))
  {
    nameoffile <- paste("summary_out_T_",Tsd,"_F_",Fsd,"_2.csv",sep="")
    durdata <- read.csv(file = nameoffile, header=TRUE, sep=",")

    a=c("Fsd", "onSD")
    write.table(as.matrix(t(a)), sep = ",", name2, col.names = F, row.names = F, append = T)

    aa = c(Fsd, durdata[[16,14]])
    write.table(as.matrix(t(aa)), sep = ",", name2, col.names = F, row.names = F, append = T)
  }
}