-3

I have something strange when comparing two vectors:

individuals=c("A","C","X","Z")
m=c("A", "B", "C", "D", "E", "F", "X", "Z")
individuals == m

it return: TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE

At the end is for:

which(individuals == m)  

which return: 1 7 8

What I am missing?

Sosa
  • 141
  • 9

2 Answers2

4

The result you get is indeed correct.
R compares each value pairwise:

individuals |   m   |  res
     A      |   A   |  T
     B      |   C   |  F
     C      |   X   |  F
     D      |   Z   |  F

it then recycle m:

individuals |   m   |  res
     E      |   A   |  F
     F      |   C   |  F
     X      |   X   |  T
     Z      |   Z   |  T

If you want to check if an element of m is in individuals use %in%:

m %in% individuals

# TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE
GGamba
  • 13,140
  • 3
  • 38
  • 47
2

We can use match

!is.na(match(m, individuals))
#[1]  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE

The reason for the unexpected result is how the == compares the recycled elements i.e. the values in 'individuals' are replicated to the length of the 'm' vector

rep(individuals, length.out = length(m))
#[1] "A" "C" "X" "Z" "A" "C" "X" "Z"

m==rep(individuals, length.out = length(m))
#[1]  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE
akrun
  • 874,273
  • 37
  • 540
  • 662