[R] matrix selection return types

Petr Pikal petr.pikal at precheza.cz
Thu Jun 15 16:46:50 CEST 2006


Hi

On 15 Jun 2006 at 15:41, vincent at 7d4.com wrote:

Date sent:      	Thu, 15 Jun 2006 15:41:14 +0200
From:           	vincent at 7d4.com
To:             	r-help at stat.math.ethz.ch
Subject:        	[R] matrix selection return types

> Dear Rusers,
> 
> I would like some comments about the following results
> (under R-2.2.0)
> 
>  > m = matrix(1:6 , 2 , 3)
>  > m
>       [,1] [,2] [,3]
> [1,]    1    3    5
> [2,]    2    4    6
> 
>  > z1 = m[(m[,1]==2),]
>  > z1
> [1] 2 4 6
>  > is.matrix(z1)
> [1] FALSE
> 
>  > z2 = m[(m[,1]==0),]
>  > z2
>       [,1] [,2] [,3]
>  > is.matrix(z2)
> [1] TRUE
> 
> Considered together, I'm a bit surprised about the
> returned types from z1 and z2.
> I would not have been surprised if z1 would still
> have been a matrix, or z2=NULL.

If you want z1 to be matrix see argument drop in ?"["

z1 = m[(m[,1]==2),,drop=F]

however why matrix is retained in second case i am not sure. Probably 
only if the result has exactly one dimension it is stripped from dim 
attribute by default.

HTH
Petr


> 
> There is certainly a logic behind this choice
> but it's not very clear for me,
> so any help/comment appreciated.
> 
> Thanks
> Vincent
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz



More information about the R-help mailing list