ashim ashim - 1 month ago 6
R Question

Why do I get "warning longer object length is not a multiple of shorter object length"?

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?

Answer

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 == it will spit out a warning if it isn't a multiple to let you know that it's probably not doing what you're expecting it is doing. == does elementwise checking for equality. I suspect what you want to do if find which of the elements of dih_y2$MemeberID 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)
Comments