From @okol @ending from in@@-toulou@e@fr  Tue Nov 27 16:44:15 2018
From: @okol @ending from in@@-toulou@e@fr (Serguei Sokol)
Date: Tue, 27 Nov 2018 16:44:15 +0100
Subject: [Rd] Subsetting row in single column matrix drops names in
 resulting vector
In-Reply-To: <20181127144836.GA16642@mail.cs.toronto.edu>
References: <mailman.47935.5.1543316402.731.r-devel@r-project.org>
 <20181127144836.GA16642@mail.cs.toronto.edu>
Message-ID: <90e7ca06-ad20-4451-948a-26611838e035@insa-toulouse.fr>

The reason that multi-[column|row] and one-[column|row] matrices should 
be treated in the same way as to names kept in the result sounds good to 
me. I withdraw my remark.

Serguei.

Le 27/11/2018 ? 15:48, Radford Neal a ?crit?:
>>> 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
> cases.
>
> 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
>


-- 
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 at insa-toulouse.fr
http://www.lisbp.fr



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