in R, I would like to find out whether there are successive repetitions in my data.
A <- c(1,2,3,3,4)
B <- c(1,2,3,4,3)
For A, I want to get TRUE, since there are two 3s directly one after the other.
For B, I want to get FALSE because the 3s are separated by the 4.
Thanks community! pointingeye
You can use rle
for this:
> rle(A)
Run Length Encoding
lengths: int [1:4] 1 1 2 1
values : num [1:4] 1 2 3 4
> any(rle(A)$lengths > 1)
[1] TRUE
> any(rle(B)$lengths > 1)
[1] FALSE
Try rle
:
any(rle(A)$lengths > 1)
#[1] TRUE
any(rle(B)$lengths > 1)
#[1] FALSE
Alternative solution (diff
):
any(diff(A)==0)
#[1] TRUE
any(diff(B)==0)
#[1] FALSE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With