[英]Find consecutive values in dataframe
I have a dataframe. 我有一个数据帧。 I wish to detect consecutive numbers and populate a new column as 1 or 0. 我希望检测连续数字并将新列填充为1或0。
ID Val
1 a 8
2 a 7
3 a 5
4 a 4
5 a 3
6 a 1
Expected output 预期产出
ID Val outP
1 a 8 0
2 a 7 1
3 a 5 0
4 a 4 1
5 a 3 1
6 a 1 0
You could do this with the diff
function in combination with abs
and see whether the outcome is 1
or another value: 您可以将diff
函数与abs
结合使用,并查看结果是1
还是其他值:
d$outP <- c(0, abs(diff(d$Val)) == 1)
which gives: 这使:
> d
ID Val outP
1 a 8 0
2 a 7 1
3 a 5 0
4 a 4 1
5 a 3 1
6 a 1 0
If you only want to take decreasing consecutive values into account, you can use: 如果您只想考虑减少的连续值,可以使用:
c(0, diff(d$Val) == -1)
When you want to do this for each ID
, you can also do this in base R or with dplyr : 如果要对每个ID
执行此操作,也可以在基本R或dplyr中执行此操作 :
# base R
d$outP <- ave(d$Val, d$ID, FUN = function(x) c(0, abs(diff(x)) == 1))
# dplyr
library(dplyr)
d %>%
group_by(ID) %>%
mutate(outP = c(0, abs(diff(Val)) == 1))
We can also a faster option by comparing the previous value with current 通过比较先前的值和当前值,我们也可以选择更快的选项
with(df1, as.integer(c(FALSE, Val[-length(Val)] - Val[-1]) ==1))
#[1] 0 1 0 1 1 0
If we need to group by "ID", one option is data.table
如果我们需要按“ID”分组,则一个选项是data.table
library(data.table)
setDT(df1)[, outP := as.integer((shift(Val, fill =Val[1]) - Val)==1) , by = ID]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.