Skip to content

array indexing and which

2 messages · Werner Wernersen, Marc Schwartz

#
Hi R friends!

I am stuck with a stupid question: I can circumvent it
but I would like to 
understand why it is wrong. It would be nice if you
could give me a hint...

I have an 2D array d and do the following:
ids <- which(d[,1]>0)

then I have a vector gk with same column size as d and
do:
ids2 <- which(gk[ids]==1)

but I can't interprete the result I get in ids2.

I get the expected result when I use:
which(gk==1 & d[,1]>0)

Why is the first version wrong?

The reason why I try to use the ids vectors is that I
want to avoid recomputation.

Thanks for your help!
   Werner
#
On Sun, 2005-04-17 at 19:13 +0200, Werner Wernersen wrote:
Having a reproducible example, as per the posting guide, would be
helpful here. We'll use a contrived example that hopefully explains what
I can only presume you are seeing.
Here ids contains the indices of the values in the vector d[, 1] that
are > 0.

For example:
[,1] [,2]
[1,]    1    1
[2,]    1    1
[3,]    0    1
[4,]    0    0
[5,]    0    1
[6,]    1    0
[1] 1 2 6

Note that c(1, 2, 6) are the indices into the vector:
[1] 1 1 0 0 0 1

of the values that are > 0.
Here ids2 contains the indices of the values in gk[ids] that equal 1.
[1] 1 1 1 0 1 1
[1] 1 1 1
[1] 1 2 3

All three of the values in gk[ids] == 1.
Here you are getting the result of logically comparing the two vectors:
[1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE

AND
[1]  TRUE  TRUE FALSE FALSE FALSE  TRUE


where the result of the comparison is the index value of each pair in
the two vectors where both values are TRUE.

Thus:
[1] 1 2 6

versus:
[1] 1 2 3
It's not wrong. It is giving you what you asked for.

Your question was wrong.  :-)
HTH,

Marc Schwartz