%in%
is value matching and “returns a vector of the positions of (first) matches of its first argument in its second” (See help('%in%')
) This means you could compare vectors of different lengths to see if elements of one vector match at least one element in another. The length of output will be equal to the length of the vector being compared (the first one).
1:2 %in% rep(1:2,5) #[1] TRUE TRUE rep(1:2,5) %in% 1:2 #[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #Note this output is longer in second
==
is logical operator meant to compare if two things are exactly equal. If the vectors are of equal length, elements will be compared element-wise. If not, vectors will be recycled. The length of output will be equal to the length of the longer vector.
1:2 == rep(1:2,5) #[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE rep(1:2,5) == 1:2 #[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
1:10 %in% 3:7 #[1] FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE #is same as sapply(1:10, function(a) any(a == 3:7)) #[1] FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE
NOTE: If possible, try to use identical
or all.equal
instead of ==
and.