Selecting items from matrix in R - unexpected behaviour
I am having a problem with selecting items from a matrix in R. Example follows.
mydata <- matrix(paste(LETTERS[1:26]), nrow=104)
creates a matrix with the letters A to Z repeated four times each. When I ask to select items with the letter "G":
mydata[mydata[,1]==c("G"),]
I get four "G"s as expected. When I ask for "G" and "H"
mydata[mydata[,1]==c("G", "H"),]
I get four "G"s and four "H"s as expected. So far so good. However when I ask for "G", "H", and "I"...
mydata[mydata[,1]==c("G", "H", "I"),]
I get only TWO "G", "H" and "I"s, and the warning message:
longer object length is not a multiple of shorter object length
when I would expect to get four sets of "G", "H" and "I".
I am somehow being a numpty - can anyone point out where I am going wrong here? I have spent at least half an hour searching for the answer and am stumped.
Thanks in anticipation!
You need to use %in%
where you are now using ==
.
To expand even further on Joshua's comments, you should note that your "successful" attempt with "==" probably was not succeeding for the reasons you guessed. R was first expanding the test vector to rep(c("G","H"), 52) and then comparing elements that were exactly aligned with that longer vector. It was only because "G" and "H" were adjacent AND were aligned on odd-even positions that you got what appeared to be sensible. Adding in the extra letter messed up the "odd-even" alignment and also threw the warning. That was why Joshua was talking about the non-integer result of 104/3.
精彩评论