From r@dford @ending from c@@toronto@edu  Tue Nov 27 01:50:14 2018
From: r@dford @ending from c@@toronto@edu (Radford Neal)
Date: Mon, 26 Nov 2018 19:50:14 -0500
Subject: [Rd] Subsetting row in single column matrix drops names in
 resulting vector
In-Reply-To: <mailman.47920.7.1542884402.17320.r-devel@r-project.org>
References: <mailman.47920.7.1542884402.17320.r-devel@r-project.org>
Message-ID: <20181127005014.GA17318@mail.cs.toronto.edu>

Dmitriy Selivanov (selivanov.dmitriy at 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

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



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