[R] matching element of a vector to i-2nd element

Davide Piffer pifferdavide at gmail.com
Sun Jul 23 20:43:44 CEST 2017


I have a df with a vector v. For each element of the vector, I want to
know whether the i-2nd element is the same as the ith element. For
example:
given
v=c(A,C,D,C) the result should be:
FALSE,FALSE,FALSE,TRUE.

I attempted something using indexing in a for loop such as (bad,
incorrect example):
 for (i in v){
  if [i]==[i-2] print T
else print F
}

However, this is obviously wrong.
Can someone provide a nice way to solve this?



More information about the R-help mailing list