我想根据另一个变量(组)对一列(按ID)求和。以我们为例:
ID t group
1 12 1
1 14 1
1 2 6
2 0.5 7
2 12 1
3 3 1
4 2 4
仅在group == 1时,我想对每个ID的t列的值分别求和,并获得:
ID t group sum
1 12 1 26
1 14 1 26
1 2 6 NA
2 0.5 7 NA
2 12 1 12
3 3 1 3
4 2 4 NA
答案 0 :(得分:2)
使用dplyr
,
df %>%
group_by(ID) %>%
mutate(new = sum(t[group == 1]),
new = replace(new, group != 1, NA))
给出,
# A tibble: 7 x 4 # Groups: ID [4] ID t group new <int> <dbl> <int> <dbl> 1 1 12 1 26 2 1 14 1 26 3 1 2 6 NA 4 2 0.5 7 NA 5 2 12 1 12 6 3 3 1 3 7 4 2 4 NA
答案 1 :(得分:1)
请考虑将基数R与ifelse
和ave()
进行条件内联聚合。
df$sum <- with(df, ifelse(group == 1, ave(t, ID, group, FUN=sum), NA))
df
# ID t group sum
# 1 1 12.0 1 26
# 2 1 14.0 1 26
# 3 1 2.0 6 NA
# 4 2 0.5 7 NA
# 5 2 12.0 1 12
# 6 3 3.0 1 3
# 7 4 2.0 4 NA
答案 2 :(得分:0)
我们可以使用data.table
方法。将'data.frame'转换为'data.table'(setDT(df)
),按'ID'分组,用逻辑表达式i
指定group ==1
,得到{{1} }(t),并将其分配(sum
)到'new'。默认情况下,其他行默认分配给:=
NA
library(data.table)
setDT(df)[group == 1, new := sum(t), ID]
df
# ID t group new
#1: 1 12.0 1 26
#2: 1 14.0 1 26
#3: 1 2.0 6 NA
#4: 2 0.5 7 NA
#5: 2 12.0 1 12
#6: 3 3.0 1 3
#7: 4 2.0 4 NA