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
is this what you want?> vector1[1] 65 1 34 100 42 20 79 43 89 10> vector2[1] 34 65 47 91 48 32 23 74 92 86> > for (i in 1:10) {+ for (j in 1:10) { + if (vector1[i] == vector2[j]) + show(c(i,j)) + } + } [1] 1 2 [1] 3 1 On Sat, Nov 8, 2008 at 8:22 AM, David Croll <david.croll at gmx.ch> 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. >
To answer the actual question, your i and j indices are not what you expect (but are what you specify). Consider the following, which follows your algorithm: > vector1 <- sample(1:100,2) > vector2 <- sample(1:100,2) > for (i in vector1) { + for (j in vector2) { + show(c(i, j)) + } + } [1] 37 68 [1] 37 1 [1] 46 68 [1] 46 1 > vector1[37] == vector2[68] [1] NA > Does this now answer your question? Ray Brownrigg MSCS, Victoria University of Wellington cruz wrote:> is this what you want? > >> vector1 > [1] 65 1 34 100 42 20 79 43 89 10 >> vector2 > [1] 34 65 47 91 48 32 23 74 92 86 >> for (i in 1:10) { > + for (j in 1:10) { > + if (vector1[i] == vector2[j]) > + show(c(i,j)) > + } > + } > [1] 1 2 > [1] 3 1 > > > > On Sat, Nov 8, 2008 at 8:22 AM, David Croll <david.croll at gmx.ch> 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 >>
It would be useful to learn how to use debug/browser. Here is the state of the machine when the error occurs:> test()Error in if (vector1[i] == vector2[j]) { : missing value where TRUE/FALSE needed Enter a frame number, or 0 to exit 1: test() Selection: 1 Called from: eval(expr, envir, enclos) Browse[1]> ls() [1] "i" "j" "vector1" "vector2" Browse[1]> vector1 [1] 27 37 57 89 20 86 97 62 58 6 Browse[1]> vector2 [1] 21 18 68 38 74 48 98 93 35 71 Browse[1]> vector1[27] [1] NA Browse[1]> Notice that vector1 is only 10 long and then you try to address vector1[27] you get an NA On Fri, Nov 7, 2008 at 7:22 PM, David Croll <david.croll at gmx.ch> 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
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.