[Rd] Subsetting row in single column matrix drops names in resulting vector

Serguei Sokol @okol @ending from in@@-toulou@e@fr
Tue Nov 27 10:57:10 CET 2018


Le 27/11/2018 à 01:50, Radford Neal a écrit :
> Dmitriy Selivanov (selivanov.dmitriy using gmail.com) wrote:
>
>> Consider following example:
>>
>> a = matrix(1:2, nrow = 2, dimnames = list(c("row1", "row2"), c("col1")))
>> a[1, ]
>> # 1
>>
>> It returns *unnamed* vector `1` where I would expect named vector. In fact
>> it returns named vector when number of columns is > 1.
>> Same issue applicable to single row matrix. Is it a bug? looks very
>> counterintuitive.
> This and related issues are addressed in pqR, in the new
> release of 2018-11-18.  (See pqR-project.org, and my blog
> post at radfordneal.wordpress.com)
>
> The behaviour of a[1,] is unchanged, for backwards compatibility
> reasons.  But in pqR one can explicitly mark an argument as
> missing using "_".  When an array subscript is missing in this way,
> the names will not be dropped in this context even if there is
> only one of them.  So a[1,_] will do what you want:
>
>    > a = matrix(1:2, nrow = 2, dimnames = list(c("row1", "row2"), c("col1")))
>    > a[1, ]
>    [1] 1
>    > a[1,_]
>    col1
>       1
To my mind, it's rather counterintuitive as

> a[2,_]
col1
    1
so a[1,_] and a[2,_] have the same name. To make it intuitive (at least for me ;) )
it should rather return names "row1" and "row2" respectively.

Best,
Serguei.
  

>
> Furthermore, pqR will not drop names when the subscript is a
> 1D array (ie, has a length-1 dim attribute) even if it is only
> one long.  In pqR, sequences that are 1D arrays are easily created
> using the .. operator.  So the following works as intended when ..
> is used, but not when the old : operator is used:
>
>    > a = matrix(1:4, nrow=2, dimnames=list(c("row1","row2"),c("col1","col2")))
>    > n = 2
>    > a[1,1:n]
>    col1 col2
>       1    3
>    > a[1,1..n]
>    col1 col2
>       1    3
>    > n = 1
>    > a[1,1:n]
>    [1] 1
>    > a[1,1..n]
>    col1
>       1
>
> You can read more about this in my blog post at
>
> https://radfordneal.wordpress.com/2016/06/25/fixing-rs-design-flaws-in-a-new-version-of-pqr/
>
> That was written when most of these features where introduced,
> though getting your specific example right relies on another
> change introduced in the most recent version.
>
>      Radford Neal
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>


-- 
Serguei Sokol
Ingenieur de recherche INRA

Cellule mathématiques
LISBP, INSA/INRA UMR 792, INSA/CNRS UMR 5504
135 Avenue de Rangueil
31077 Toulouse Cedex 04

tel: +33 5 62 25 01 27
email: sokol using insa-toulouse.fr
http://www.lisbp.fr



More information about the R-devel mailing list