数据条件表的最大分组条件

编程入门 行业动态 更新时间:2024-10-26 20:33:36
本文介绍了数据条件表的最大分组条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这样的数据:

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

更多推荐

数据条件表的最大分组条件

本文发布于:2023-11-22 00:50:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!