在不使用dplyr的情况下,使用带有R的aggregate()函数在特定条件下聚合行

时间:2019-07-12 10:09:58

标签: r aggregate vectorization

我想在某些条件下汇总表中的行。例如,我有:

x <- data.frame("id"=c("T","T","R","R"),"value"=c(10,-5,10,-5),"level"=c(3,2,1,2))
print(x)

我的条件是:对于相同的“ id”,如果负值的水平低于正值的水平,则可以通过求和来求和。所以我得到了:

x <- data.frame("id"=c("T","R","R"),"value"=c(5,10,-5))
print(x)

我可以使用aggregate()功能吗?

2 个答案:

答案 0 :(得分:1)

或者:

x <- data.frame("id"=c("T","T","R","R"),"value"=c(10,-5,10,-5),"level"=c(3,2,1,2))

lookup_vec <- setNames(x[sign(x$value) == 1, ]$level,
                       as.character(x[sign(x$value) == 1, ]$id))
x$level_plus <- lookup_vec[as.character(x$id)]
x$level_plus <- ifelse(x$level_plus >= x$level, x$level_plus, x$level)
aggregate(value ~ id + level_plus, x, sum)[c("id", "value")]
# id value
# 1  R    10
# 2  R    -5
# 3  T     5

答案 1 :(得分:0)

您可以使用by

do.call(rbind, by(x, x$id, function(x) {i <- cbind(x, d=c(1, diff(x[, 3]))); i[i$d > 0, 1:2]}))
#   id value
# 1  T     5
# 2  R    10
# 3  R    -5