I would like to check if the elements of a vector are equal to the elements of another vector +/- 1, and count the number of times this is true. I could do it manually this way:
> a <- c(1:10)
> b <- c(1,2,3,4,6,5,10,11,12,13)
> sum(a == b-1) + sum(a == b) + sum(a == b+1)
[1] 6
Is there a neater way to achieve this? In the code I'm trying to write I'm using fairly long index vectors for both a
and b
, so the above would look quite messy.