简体   繁体   English

r汇总动态列

[英]r aggregate dynamic columns

I'd like to create an aggregation without knowing neither the column names nor their positions ie. 我想创建一个既不知道列名也不知道它们位置的聚合。 I retrieve the names dynamically. 我动态检索名称。

Further I'm able to use data.frame or data.table as I'm forced to use R version 3.1.1 此外,由于必须使用R版本3.1.1,因此我可以使用data.frame或data.table

Is there an option like do.call... as explained in this answer for 'order' 是否有像do.call ...这样的选项,如“订单”的此答案中所述

trying a similar do.call with 'aggregate' leads to an error 尝试使用“ 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)

or is there an option with data.table? 还是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]

Thanks for advice! 谢谢你的建议!

aggregate can take a formula, and you can build a formula from a string. 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM