[英]Finding groups of numbers within a row across columns above a specific threshold
I'm trying to identify "responders" in a data set as defined by a patient having a certain clinical metric be >= 2 across 4 weeks.我正在尝试识别数据集中的“响应者”,该数据集中由具有特定临床指标的患者在 4 周内 >= 2 定义。
I have a data set for patients of the below format, but for many more:我有以下格式的患者数据集,但还有更多:
pt_num week_0 week_1 week_2 week_3 week_4 week_5 week_6 week_7 week_8
1 0 2 3 3 4 3 2 1 1
2 0 2 3 3 1 3 2 1 1
In the example above patient 1 would be a responder, patient 2 would not.在上面的示例中,患者 1 将是响应者,而患者 2 则不会。
I have an idea on how to do this through looping over every row and cell - but I'm hoping there is a much more efficient way to do this - any help appreciated.我对如何通过遍历每一行和单元格来做到这一点有一个想法——但我希望有一种更有效的方法来做到这一点——任何帮助表示赞赏。
We can use rle
row-wise and check if there is at least one instance of 4 consecutive weeks where a metric is greater than equal to 2.我们可以逐行使用
rle
并检查是否至少有一个连续 4 周的实例,其中指标大于等于 2。
apply(df[-1], 1, function(x) any(rle(x >= 2)$lengths >= 4))
#[1] TRUE FALSE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.