Dmitriy Selivanov
2018-Nov-21 16:51 UTC
[Rd] Subsetting row in single column matrix drops names in resulting vector
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]]
Rui Barradas
2018-Nov-21 16:54 UTC
[Rd] Subsetting row in single column matrix drops names in resulting vector
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. > >
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]]
Apparently Analagous 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
- embedding data frame in R code?