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

Aggregate rows under certain conditions using aggregate() function with R, without using dplyr

我想在特定条件下聚合 table 中的行。例如我有:

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() 函数来做到这一点吗?

你可以使用 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

或者:

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