57

I have dataframe dih_y2. These two lines give me a warning:

> memb = dih_y2$MemberID[1:10]
> dih_col = which(dih_y2$MemberID == memb)  
Warning message:
In dih_y2$MemberID == memb :
longer object length is not a multiple of shorter object length

Why?

vard
  • 4,057
  • 2
  • 26
  • 46
ashim
  • 24,380
  • 29
  • 72
  • 96

4 Answers4

94

You don't give a reproducible example but your warning message tells you exactly what the problem is.

memb only has a length of 10. I'm guessing the length of dih_y2$MemberID isn't a multiple of 10. When using ==, R spits out a warning if it isn't a multiple to let you know that it's probably not doing what you're expecting it to do. == does element-wise checking for equality. I suspect what you want to do is find which of the elements of dih_y2$MemberID are also in the vector memb. To do this you would want to use the %in% operator.

dih_col <- which(dih_y2$MemeberID %in% memb)
MBorg
  • 1,345
  • 2
  • 19
  • 38
Dason
  • 60,663
  • 9
  • 131
  • 148
34

When you perform a boolean comparison between two vectors in R, the "expectation" is that both vectors are of the same length, so that R can compare each corresponding element in turn.

R has a much loved (or hated) feature called recycling, whereby in many circumstances if you try to do something where R would normally expect objects to be of the same length, it will automatically extend, or recycle, the shorter object to force both objects to be of the same length.

If the longer object is a multiple of the shorter, this amounts to simply repeating the shorter object several times. Oftentimes R programmers will take advantage of this to do things more compactly and with less typing.

But if they are not multiples, R will worry that you may have made a mistake, and perhaps didn't mean to perform that comparison, hence the warning.

Explore yourself with the following code:

> x <- 1:3
> y <- c(1,2,4)
> x == y
[1]  TRUE  TRUE FALSE
> y1 <- c(y,y)
> x == y1
[1]  TRUE  TRUE FALSE  TRUE  TRUE FALSE
> y2 <- c(y,2)
> x == y2
[1]  TRUE  TRUE FALSE FALSE
Warning message:
In x == y2 :
  longer object length is not a multiple of shorter object length
Chris
  • 6,302
  • 1
  • 27
  • 54
joran
  • 169,992
  • 32
  • 429
  • 468
  • I can understand why R warns you. I just don't get why these two operators should return two different results. Can anyone kindly explain this? – sarovasta Feb 10 '20 at 09:50
16

I had a similar issue and using %in% operator instead of the == (equality) operator was the solution:

# %in%

Hope it helps.

CKE
  • 1,533
  • 19
  • 18
  • 29
landrower
  • 475
  • 6
  • 11
0

I had a similar problem but it had to do with the structure and class of the object. I would check how dih_y2$MemberID is formatted.

EDennnis
  • 191
  • 1
  • 11