missing value where TRUE/FALSE needed
They may be the same length; that's not what the error message is complaining about: it says there is a missing value (i.e., an NA) where a TRUE/FALSE value is needed, therefore the 'if' doesn't know what to do, since it is not TRUE or FALSE. So, try summary(vector1) summary(vector2) sum(is.na(vector1)) sum(is.na(vector2)) to see if/where/why there are NAs in these vectors.
David Croll wrote:
Hello dear R people,
for my MSc thesis I need to program some functions, and some of them
simply do not work. In the following example, I made sure both vectors
have the same length (10), but R gives me the following error:
Error in if (vector1[i] == vector2[j]) { :
missing value where TRUE/FALSE needed
I googled for possible solutions, but I did not find a good explanation
for this...
The code:
test <- function() {
vector1 <- sample(1:100,10)
vector2 <- sample(1:100,10)
for (i in vector1) {
for (j in vector2) {
if (vector1[i] == vector2[j]) {
show(list(i,j))
}
}
}
}
Regards, David
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.