R:子集增加值到最大值不包括减少 [英] R: Subsetting on increasing value to max excluding the decreasing
本文介绍了R:子集增加值到最大值不包括减少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我进行了多次试验,其中一个变量增加到感兴趣的最大值,然后又减少到起点.我将如何仅保留将值增加到最大值的观察结果.谢谢.
I have a number of trials where one variable increases to a max of interest then decreases back to a starting point. How would I go about just retaining the observations with the increasing values to max. Thanks.
例如
Trial A B C
1 2 4 1
1 4 3 2
1 3 7 3
1 3 3 2
1 4 1 1
2 4 1 1
2 6 2 2
2 3 1 3
2 1 1 2
2 7 3 1
...
所以我们将检查 C 上的最大值并保留如下,
So we would check max on C and retain as follows,
Trial A B C
1 2 4 1
1 4 3 2
1 3 7 3
2 4 1 1
2 6 2 2
2 3 1 3
...
最终我会有一个较低的截止值,并且可能会改变我所说的最大值的意思,但基本上以上就是目标.
Ultimately I'll have a low cut off value as well as varying perhaps what I mean by max but essentially the above is the aim.
推荐答案
可能不是最有效的解决方案,但这里尝试使用 data.table
Probably not the most efficient solution, but here is an attempt using data.table
library(data.table)
setDT(df)[, .SD[1:which.max(C)], by = Trial]
# Trial A B C
# 1: 1 2 4 1
# 2: 1 4 3 2
# 3: 1 3 7 3
# 4: 2 4 1 1
# 5: 2 6 2 2
# 6: 2 3 1 3
或者为了提高效率
indx <- setDT(df)[, .I[1:which.max(C)], by = Trial]
df[indx$V1]
这篇关于R:子集增加值到最大值不包括减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文