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

问题描述 投票:1回答:1

我想从set 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

有什么我想念的吗?不应该追加=要照顾吗?

r append write.table
1个回答
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)
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.