I’m trying to test if all values in an object (if ordered) are adjacent integer values. For example:
x <- c(1,2,3)
is.adjacent(x)
TRUE
y <- c(1,2,4)
is.adjacent(y)
FALSE
z <- c(4,2,1,3)
is.adjacent(z)
TRUE
Any thoughts on a good approach?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here’s a solution. I’ve constructed it so that it will return
TRUEfor a vector that contains a set of consecutive integers, even it some of them are repeated (e.g.c(1,3,2,1,1,1)). If you would like it to returnFALSEin such cases, just remove the part that callsunique().