【发布时间】:2020-02-18 12:27:11
【问题描述】:
我有一个 for 循环,它遍历不同 CSV 文件中的特定列(所有这些不同的文件只是特定类的不同运行)并检索每个值的计数。例如在第一个文件中(第一次运行):
0 1 67
101 622 277
第二次运行:
0 1 67 68
109 592 297 2
第三次运行:
0 1 67
114 640 246
请注意,每次运行可能会产生不同的值(查看第二次运行,其中包含另一个值,即 68)。我想将所有这些结果合并到一个列表中,然后将其写入 CSV 文件。为此,我执行了以下操作:
files <- list.files("/home/adam/Desktop/runs", pattern="*.csv", recursive=TRUE, full.names=TRUE, include.dirs=TRUE)
all <- list()
col <- 14
for(j in 1:length(files)){
dataset <- read.csv(files[j])
uniqueValues <- table(dataset[,col]) #this generates the examples shown above
all <- rbind(uniqueValues)
}
write.table(all, "all.csv", col.names=TRUE, sep=",")
all 的结果是:
0 1 67
114 640 246
如何解决?
预期结果:
0 1 67 68
101 622 277 0
109 592 297 2
114 640 246 0
【问题讨论】: