r 聚合动态列

r aggregate dynamic columns

我想在不知道列名及其位置的情况下创建一个聚合,即。我动态检索名称。

此外,我可以使用 data.frame 或 data.table,因为我被迫使用 R 版本 3.1.1

有没有类似do.call的选项...as explained in this answer for 'order'

用 'aggregate' 尝试类似的 do.call 会导致错误

# generate a small dataset
set.seed(1234)
smalldat <- data.frame(group1 = rep(1:2, each = 5), 
                       group2 = rep(c('a','b'), times = 5), 
                       x = rnorm(10),
                       y = rnorm(10))

group_by <- c('group1','group2')

test <- do.call( aggregate.data.frame , c(by=group_by, x=smalldat, FUN=mean))
#output
#Error in is.data.frame(x) : Argument "x" missing (no default)

或者有 data.table 选项吗?

# generate a small dataset
set.seed(1234)
smalldat <- data.frame(group1 = rep(1:2, each = 5), 
                       group2 = rep(c('a','b'), times = 5), 
                       x = rnorm(10),
                       y = rnorm(10))


# convert to data.frame to data.table
library(data.table)
smalldat <- data.table(smalldat)

# convert aggregated variable into raw data file

smalldat[, aggGroup1 := mean(x), by = group1]

感谢指教!

aggregate 可以采用公式,也可以从字符串构建公式。

form = as.formula(paste(". ~", paste(group_by, collapse = " + ")))
aggregate(form, data = smalldat, FUN = mean)
#   group1 group2          x           y
# 1      1      a  0.1021667 -0.09798418
# 2      2      a -0.5695960 -0.67409059
# 3      1      b -1.0341342 -0.46696381
# 4      2      b -0.3102046  0.46478476