[R] missing value where TRUE/FALSE needed

Ray Brownrigg ray at mcs.vuw.ac.nz
Sat Nov 8 02:46:27 CET 2008


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
>>



More information about the R-help mailing list