简体   繁体   English

R:基于特定条件聚合数据帧

[英]R: aggregate a data frame based on certain condition

I have a data frame. 我有一个数据框。 I want to aggregate one column of it based on another list. 我想基于另一个列表聚合它的一列。

df<-data.frame(X=c("a", "b", "c", "d"), Y=c(0.5, 0.4, 0.01, 0.09))
X     Y
a     0.5
b     0.4
c     0.01
d     0.09

l<-c("a", "c", "d")

l is the list which needs to grouped together. l是需要组合在一起的列表。 So, here I want to group all the elements in df$X that are there in l. 所以,在这里我想把df$X所有元素分组在l中。

My desired result is:
X     Y
a'    0.6
b     0.4

Any idea on how to do this? 有关如何做到这一点的任何想法?

Thanks. 谢谢。

We can "temporarily" change the relevant X values to the same grouping variable and then aggregate. 我们可以“暂时”将相关的X值更改为相同的分组变量,然后进行汇总。 Here I arbitrarily choose l[1] , which also happens to be "a" 在这里我随意选择l[1] ,这也恰好是"a"

aggregate(Y ~ X, within(df, X <- replace(X, X %in% l, l[1])), sum)
#   X   Y
# 1 a 0.6
# 2 b 0.4

One upside to this use of within() in the aggregate() call is that the original df will remain unchanged. aggregate()调用中使用within()一个好处是原始df将保持不变。

An option using data.table 使用data.table的选项

library(data.table)
df1 <- copy(df)
setkey(setDT(df1), X)[l, X:='a'][, list(Y=sum(Y)), X]
#  X   Y
#1: a 0.6
#2: b 0.4

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

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