简体   繁体   中英

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. So, here I want to group all the elements in df$X that are there in 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. Here I arbitrarily choose l[1] , which also happens to be "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.

An option using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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