[R] array indexing and which

Marc Schwartz MSchwartz at MedAnalytics.com
Sun Apr 17 20:49:43 CEST 2005


On Sun, 2005-04-17 at 19:13 +0200, Werner Wernersen wrote:
> 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...

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.

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

Here ids contains the indices of the values in the vector d[, 1] that
are > 0.

For example:

> d <- matrix(sample(0:1, 12, replace = TRUE), ncol = 2)
> d
     [,1] [,2]
[1,]    1    1
[2,]    1    1
[3,]    0    1
[4,]    0    0
[5,]    0    1
[6,]    1    0

> ids <- which(d[, 1] > 0)
> ids
[1] 1 2 6

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

> d[, 1]
[1] 1 1 0 0 0 1

of the values that are > 0.

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

Here ids2 contains the indices of the values in gk[ids] that equal 1.

> gk <- sample(0:1, 6, replace = TRUE)
> gk
[1] 1 1 1 0 1 1


> gk[ids]  # same as gk[c(1, 2, 6)]
[1] 1 1 1

> ids2 <- which(gk[ids] == 1)
> ids2
[1] 1 2 3

All three of the values in 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)

Here you are getting the result of logically comparing the two vectors:

> gk == 1
[1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE

AND

> d[, 1] > 0
[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:

> which(gk == 1 & d[, 1] > 0)
[1] 1 2 6

versus:

> ids2
[1] 1 2 3

> Why is the first version wrong?

It's not wrong. It is giving you what you asked for.

Your question was wrong.  :-)

> The reason why I try to use the ids vectors is that I
> want to avoid recomputation.
> 
> Thanks for your help!
>    Werner

HTH,

Marc Schwartz




More information about the R-help mailing list