Radford Neal
2018-Nov-27 14:48 UTC
[Rd] Subsetting row in single column matrix drops names in resulting vector
> > 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
2018-Nov-27 15:44 UTC
[Rd] Subsetting row in single column matrix drops names in resulting vector
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
Seemingly Similar Threads
- Subsetting row in single column matrix drops names in resulting vector
- Subsetting row in single column matrix drops names in resulting vector
- Subsetting row in single column matrix drops names in resulting vector
- create a data.frame for aov
- header line generated write.table