R dplyr窗口函数,获取下一个满足某些条件的x窗口中的第一个值 [英] R dplyr window function, get the first value in the next x window that fulfil some condition
问题描述
我有一些dplyr数据框,并且有一些条件.我想知道每个单元格与下一个x行中符合条件的第一个单元格的索引是什么.
I have some dplyr dataframe and I have some condition. I want to know for each cell what is the index of the first cell that matches the condition in the next x rows.
在我的情况下,我希望有一个附加列,该列包含第一个值的索引,该索引至少比z中的当前值大.
In my case, I want to have an additional column that holds the index of the first value that was larger than the current value in at least z.
示例:在这里,我们正在寻找下3行中第一个值的索引,该索引比当前值大至少3倍.在第一行的情况下,该值为0,并且接下来的3个单元格中的至少大于3的第一个值为单元格编号4,其值为= 3.
Example: here we are looking for the index of the first value in the next 3 rows that is larger by at least 3 from the current value. In the case of the first row, the value is 0 and the first value in the next 3 cells that is larger by at least 3 is cell number 4 where its value = 3.
在第三行中,值= 2,在接下来的3行中,没有与条件匹配的值,因此我们得到的值为NA
In the third row, the value = 2 and in the next 3 rows there is no value that matches the condition so we get a value of NA
value index_of_matched_cell
1 0 4
2 0 4
3 2 NA
4 3 7
5 3 7
6 3 7
7 6 NA
8 6 NA
9 6 NA
谢谢!
推荐答案
这是使用 zoo
中的 rollapply
的一种方法:
Here is one way using rollapply
from zoo
:
next_rows <- 3
larger_than <- 3
with(df, zoo::rollapply(seq_along(value), next_rows + 1, function(x)
x[which(value[x] >= (value[x[1]] + larger_than))[1]],
align = 'left', fill = NA))
#[1] 4 4 NA 7 7 7 NA NA NA
在 rollapply
中,我们迭代窗口大小为 next_rows + 1
的每一行的索引(因为我们要考虑接下来的3行和 rollapply 代码>也会考虑当前行).我们将当前的
value
与接下来的3个值进行比较,并返回大于或等于 larger_than
值的第一个索引,并返回其索引.
In rollapply
we iterate over the index of each row with window size of next_rows + 1
(since we want to consider next 3 rows and rollapply
also considers current row). We compare the current value
with next 3 values and return the first index where it is greater or equal to than larger_than
value and return it's index.
这篇关于R dplyr窗口函数,获取下一个满足某些条件的x窗口中的第一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!