[R] which on array

Uwe Ligges
Fri, 02 Apr 2004 11:39:37 -0000

asemeria at cramont.it wrote:

>
>
>
> Good morning !
> Today I found a strange, for my poor knowledge of R, behaviour of
> 'which' on a matrix:
>
> HAL9000> str(cluster.matrix)
>  num [1:227, 1:6300] 2 2 2 2 2 2 2 2 2 2 ...
>
> HAL9000> class(cluster.matrix)
> [1] "matrix"
>
> HAL9000> ase <- cluster.matrix[1:5,1:5]
>
> HAL9000> ase
> [,1] [,2] [,3] [,4] [,5]
> [1,]    2    2    2    0   -2
> [2,]    2    2    2    0   -2
> [3,]    2    2    2    0   -2
> [4,]    2    2    2    0   -2
> [5,]    2    2    2    0   -2
>
> HAL9000> ase2 <- matrix(c(2,2,2,2,2)%*%t(c(1,1,1,0,-1)),5,5)
>
> HAL9000> ase2
>      [,1] [,2] [,3] [,4] [,5]
> [1,]    2    2    2    0   -2
> [2,]    2    2    2    0   -2
> [3,]    2    2    2    0   -2
> [4,]    2    2    2    0   -2
> [5,]    2    2    2    0   -2
>
> HAL9000> str(ase2)
>  num [1:5, 1:5] 2 2 2 2 2 2 2 2 2 2 ...
>
> But now:
>
> HAL9000> which(ase2==-2,arr.ind=T)
>      row col
> [1,]   1   5
> [2,]   2   5
> [3,]   3   5
> [4,]   4   5
> [5,]   5   5
>
> HAL9000> which(ase==-2,arr.ind=T)
> numeric(0)
>
> which(ase=="-2",arr.ind=T)
>      row col
> [1,]   1   5
> [2,]   2   5
> [3,]   3   5
> [4,]   4   5
> [5,]   5   5
>
>
> May be the original matrix 'cluster.matrix' is not
> properly a numerical matrix!? But 'str' say num[...] !!??
> Some suggestion.

My guess is that cluster.matrix, hence ase, has been generated in some
calculation, and its numerical floating point representation is a bit
off the expected integer. Use identical(), all.equal() and friends to
analyse it.

Uwe Ligges


> Tks!
>
>
> A.S.
>
> ----------------------------
>
> Alessandro Semeria
> Models and Simulations Laboratory
> Montecatini Environmental Research Center (Edison Group),
> Via Ciro Menotti 48,
> 48023 Marina di Ravenna (RA), Italy
> Tel. +39 544 536811
> Fax. +39 544 538663
> E-mail: alessandro.semeria at cramont.it
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!