Skip to content

Subsetting row in single column matrix drops names in resulting vector

2 messages · Radford Neal, Serguei Sokol

#
Dmitriy Selivanov (selivanov.dmitriy at gmail.com) wrote:

            
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

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
#
Le 27/11/2018 ? 01:50, Radford Neal a ?crit?:
To my mind, it's rather counterintuitive as
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.