Dmitriy Selivanov
2018-Nov-21 16:58 UTC
[Rd] Subsetting row in single column matrix drops names in resulting vector
Hi Rui. Thanks for answer, I'm aware of drop = FALSE option. Unfortunately it doesn't resolve the issue - I'm expecting to get a vector, not a matrix . ??, 21 ????. 2018 ?. ? 20:54, Rui Barradas <ruipbarradas at sapo.pt>:> Hello, > > Use drop = FALSE. > > a[1, , drop = FALSE] > # col1 > #row1 1 > > > Hope this helps, > > Rui Barradas > > ?s 16:51 de 21/11/2018, Dmitriy Selivanov escreveu: > > Hello here. I'm struggling to understand R's subsetting behavior in > couple > > of edge cases - subsetting row in a single column matrix and subsetting > > column in a single row matrix. I've read R's docs several times and > haven't > > found answer. > > > > 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. > > > > >-- Regards Dmitriy Selivanov [[alternative HTML version deleted]]
Emil Bode
2018-Nov-22 13:47 UTC
[Rd] Subsetting row in single column matrix drops names in resulting vector
The problem is that the drop is only applied (or not) after the subsetting, so what R does is: - Getting the subset, which means a 1 x 1 matrix. - Only then It either returns that as is (when drop=FALSE), or removes ALL dimensions of extent 1, regardless of whether these are rows or columns (or higher dimensions). And it can't keep any names, because what name should be returned? The name 'row1' is just as valid as 'col1'. I guess if we could design everything anew, a solution would be to be able to specify something like a[1,,drop='row'], or a[1,,drop=1] to drop the rows but keep columns, and get a vector being equal to 'row1' (which in this case just has length-1, and names 'col1') That not how it's designed, but you could use 'adrop()' from the 'abind' package: abind:: adrop(a[1,,drop=FALSE], drop=1) first subsets, then drops the row-dimension, so gives what you're looking for. Hope this solves your problem. Best regards, Emil Bode ?On 21/11/2018, 17:58, "R-devel on behalf of Dmitriy Selivanov" <r-devel-bounces at r-project.org on behalf of selivanov.dmitriy at gmail.com> wrote: Hi Rui. Thanks for answer, I'm aware of drop = FALSE option. Unfortunately it doesn't resolve the issue - I'm expecting to get a vector, not a matrix . ??, 21 ????. 2018 ?. ? 20:54, Rui Barradas <ruipbarradas at sapo.pt>: > Hello, > > Use drop = FALSE. > > a[1, , drop = FALSE] > # col1 > #row1 1 > > > Hope this helps, > > Rui Barradas > > ?s 16:51 de 21/11/2018, Dmitriy Selivanov escreveu: > > Hello here. I'm struggling to understand R's subsetting behavior in > couple > > of edge cases - subsetting row in a single column matrix and subsetting > > column in a single row matrix. I've read R's docs several times and > haven't > > found answer. > > > > 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. > > > > > -- Regards Dmitriy Selivanov [[alternative HTML version deleted]] ______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Serguei Sokol
2018-Nov-22 14:34 UTC
[Rd] Subsetting row in single column matrix drops names in resulting vector
Le 22/11/2018 ? 14:47, Emil Bode a ?crit?:> The problem is that the drop is only applied (or not) after the subsetting, so what R does is: > - Getting the subset, which means a 1 x 1 matrix. > - Only then It either returns that as is (when drop=FALSE), or removes ALL dimensions of extent 1, regardless of whether these are rows or columns (or higher dimensions). > And it can't keep any names, because what name should be returned? The name 'row1' is just as valid as 'col1'.If it is the only reason to not return any name in this case, I could make a suggestion. Let return the name corresponding to the index in subsetting request, i.e. for a one-column matrix example it would give names(a[1,]) #"row1" names(a[2,]) #"row2" as the indexes 1 and 2 here above corresponds to rows. Just my 0.02? Serguei.> I guess if we could design everything anew, a solution would be to be able to specify something like a[1,,drop='row'], or a[1,,drop=1] to drop the rows but keep columns, and get a vector being equal to 'row1' (which in this case just has length-1, and names 'col1') > That not how it's designed, but you could use 'adrop()' from the 'abind' package: > abind:: adrop(a[1,,drop=FALSE], drop=1) first subsets, then drops the row-dimension, so gives what you're looking for. > Hope this solves your problem. > > Best regards, > Emil Bode > > > ?On 21/11/2018, 17:58, "R-devel on behalf of Dmitriy Selivanov" <r-devel-bounces at r-project.org on behalf of selivanov.dmitriy at gmail.com> wrote: > > Hi Rui. Thanks for answer, I'm aware of drop = FALSE option. Unfortunately > it doesn't resolve the issue - I'm expecting to get a vector, not a matrix . > > ??, 21 ????. 2018 ?. ? 20:54, Rui Barradas <ruipbarradas at sapo.pt>: > > > Hello, > > > > Use drop = FALSE. > > > > a[1, , drop = FALSE] > > # col1 > > #row1 1 > > > > > > Hope this helps, > > > > Rui Barradas > > > > ?s 16:51 de 21/11/2018, Dmitriy Selivanov escreveu: > > > Hello here. I'm struggling to understand R's subsetting behavior in > > couple > > > of edge cases - subsetting row in a single column matrix and subsetting > > > column in a single row matrix. I've read R's docs several times and > > haven't > > > found answer. > > > > > > 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. > > > > > > > > > > > -- > Regards > Dmitriy Selivanov > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > > > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- 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
Dmitriy Selivanov
2018-Nov-22 19:28 UTC
[Rd] Subsetting row in single column matrix drops names in resulting vector
Emil, thanks for very nice explanation. Wish base drop would have same behavior as abind::adrop. ??, 22 ????. 2018 ?., 17:47 Emil Bode emil.bode at dans.knaw.nl:> The problem is that the drop is only applied (or not) after the > subsetting, so what R does is: > - Getting the subset, which means a 1 x 1 matrix. > - Only then It either returns that as is (when drop=FALSE), or removes ALL > dimensions of extent 1, regardless of whether these are rows or columns (or > higher dimensions). > And it can't keep any names, because what name should be returned? The > name 'row1' is just as valid as 'col1'. > I guess if we could design everything anew, a solution would be to be able > to specify something like a[1,,drop='row'], or a[1,,drop=1] to drop the > rows but keep columns, and get a vector being equal to 'row1' (which in > this case just has length-1, and names 'col1') > That not how it's designed, but you could use 'adrop()' from the 'abind' > package: > abind:: adrop(a[1,,drop=FALSE], drop=1) first subsets, then drops the > row-dimension, so gives what you're looking for. > Hope this solves your problem. > > Best regards, > Emil Bode > > > ?On 21/11/2018, 17:58, "R-devel on behalf of Dmitriy Selivanov" < > r-devel-bounces at r-project.org on behalf of selivanov.dmitriy at gmail.com> > wrote: > > Hi Rui. Thanks for answer, I'm aware of drop = FALSE option. > Unfortunately > it doesn't resolve the issue - I'm expecting to get a vector, not a > matrix . > > ??, 21 ????. 2018 ?. ? 20:54, Rui Barradas <ruipbarradas at sapo.pt>: > > > Hello, > > > > Use drop = FALSE. > > > > a[1, , drop = FALSE] > > # col1 > > #row1 1 > > > > > > Hope this helps, > > > > Rui Barradas > > > > ?s 16:51 de 21/11/2018, Dmitriy Selivanov escreveu: > > > Hello here. I'm struggling to understand R's subsetting behavior in > > couple > > > of edge cases - subsetting row in a single column matrix and > subsetting > > > column in a single row matrix. I've read R's docs several times and > > haven't > > > found answer. > > > > > > 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. > > > > > > > > > > > -- > Regards > Dmitriy Selivanov > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > >[[alternative HTML version deleted]]
Maybe Matching 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
- Subsetting row in single column matrix drops names in resulting vector
- new version of abind()