数据条件表的最大分组条件 [英] Max by Group with Condition for a data.table

查看:55
本文介绍了数据条件表的最大分组条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据:

library(data.table)
group <- c("a","a","a","b","b","b")
cond <- c("N","Y","N","Y","Y","N")
value <- c(2,1,3,4,2,5)

dt <- data.table(group, cond, value)

group cond value
a     N    2
a     Y    1
a     N    3
b     Y    4
b     Y    2
b     N    5

当整个组的cond为Y时,我想返回最大值.像这样:

I would like to return max value when the cond is Y for the entire group. Something like this:

group cond value max
a     N    2     1
a     Y    1     1
a     N    3     1
b     Y    4     4
b     Y    2     4
b     N    5     4

我尝试将ifelse条件添加到分组的max中,但是,当行不符合条件时,我最终只是返回NA的no条件:

I've tried adding an ifelse condition to a grouped max, however, I end up just returning the no condition of NA when the row doesn't meet the condition:

dt[, max := ifelse(cond=="Y", max(value), NA), by = group]

推荐答案

假定对于每个组",我们需要获取值"的 max ,其中"cond"为"Y",按组"分组后,将值"与逻辑条件( cond =='Y')进行子集并获得 max

Assuming that for each 'group' we need to get the max of 'value' where the 'cond' is "Y", after grouping by 'group', subset the 'value' with the logical condition (cond == 'Y') and get the max value

dt[, max := max(value[cond == 'Y']), by = group]
dt
#   group cond value max
#1:     a    N     2   1
#2:     a    Y     1   1
#3:     a    N     3   1
#4:     b    Y     4   4
#5:     b    Y     2   4
#6:     b    N     5   4

这篇关于数据条件表的最大分组条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆