您将如何根据文件列表创建摘要动物园?

How would you create a summary zoo from a list of files?

假设我有许多具有以下格式的 csv 文件:

Date,col1,col2,col3
8/1/2017,2,3,4
8/5/2017,4,6,7

Date,col1,col2,col3
8/1/2017,2,3,4
8/3/2017,2,5,4
8/5/2017,4,6,7

我如何创建一个 1 动物园对象,每个日期显示每列的总数?

使用上面的文件内容,我想创建以下动物园对象:

Date      | col1 | col2 | col3
8/1/2017      4     6      8
8/3/2017      2     5      4
8/5/2017      8    12     14

NOTE: I would prefer a base-R solution (using zoo package is OK)

我们可以通过 lapply

读取列表中的文件来使用 read.zoo
library(zoo)

files <- list.files("C:\test", pattern = ".*.csv", full.names = TRUE)
out.list <- lapply(files, function(x) read.zoo(x, header=TRUE, sep = ",", index.column = "Date"))
summary <- Reduce("+", do.call(merge, args = c(out.list, retclass = "list", fill = 0)))
colnames(summary) <- colnames(out.list[[1]])