Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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?

like image 744
ashim Avatar asked Jun 02 '12 19:06

ashim


People also ask

How do you fix long object length is not a multiple of shorter object length?

Fixing this problem is quite easy! All you need to do is make sure that the vectors are the same length. When you only have two vectors, it is also easy to make sure the larger length is a multiple of the smaller. This solution becomes more difficult when you use more than two vectors.

What does longer object length is not a multiple of shorter object length mean in R?

One common warning message you may encounter in R is: Warning message: In a + b : longer object length is not a multiple of shorter object length. This warning message occurs when you attempt to perform some operation across two or more vectors that don't have the same length.


1 Answers

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) 
like image 183
Dason Avatar answered Oct 03 '22 14:10

Dason