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

Radford Neal r@dford @ending from c@@toronto@edu
Tue Nov 27 15:48:36 CET 2018

> > 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.

The aim in designing these features should be to make it easier to
write reliable software, which doesn't unexpectedly fail in edge

Here, the fact that a is a matrix presumably means that the program is
designed to work for more than one column - in fact, it's likely that
the programmer was mostly thinking of the case where there is more
than one column, and perhaps only testing that case.  But of course
there is usually no reason why one column (or even zero columns) is
impossible.  We want the program to still work in such cases.

When there is more than one column, a[1,] and a[1,_] both produce a
vector with the _column_ names attached, and this is certainly not
going to change (nor should it, unless one wants to change the whole
semantics of matrices so that rows and columns are treated
non-symmetrically, and even then attaching the same row name to all
the elements would be rather strange...).

After v <- a[1,_], the program may well have an expression like v[nc]
where nc is a column name.  We want this to still work if there
happens to be only one column.  That will happen only if a[1,_]
attaches a column name, not a row name, when a has only one column.

   Radford Neal

More information about the R-devel mailing list