Bug 16297 - one.column.matrix[one.row, ] has no names
Summary: one.column.matrix[one.row, ] has no names
Alias: None
Product: R
Classification: Unclassified
Component: Misc (show other bugs)
Version: R-devel (trunk)
Hardware: All Linux
: P5 minor
Assignee: R-core
Depends on:
Reported: 2015-04-02 20:17 UTC by Toby Dylan Hocking
Modified: 2018-08-08 10:53 UTC (History)
2 users (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Toby Dylan Hocking 2015-04-02 20:17:55 UTC
When I take one row from a two-column matrix with colnames I get a named vector

> (two.columns <- matrix(1:6, 3, 2, dimnames=list(c("a", "b", "c"), c("x", "y"))))
  x y
a 1 4
b 2 5
c 3 6
> two.columns[2,]
x y 
2 5 

so why can't I get a named vector (of length 1) when I take one row from a one-column matrix?

> (one.column <- matrix(1:3, 3, 1, dimnames=list(c("a", "b", "c"), "x")))
a 1
b 2
c 3
> one.column[2,]
[1] 2

I was expecting that it would have names "x" and that the following test would pass

stopifnot(identical(names(one.column[2,]), "x"))

I did not see documentation of this behavior on help("[") so I assume it is a bug.
Comment 1 Peter Dalgaard 2015-04-02 22:03:06 UTC
Undocumented behaviour is not a bug, but the docs for drop() could be read as indicating that the name of the result should be "b":

  if the result is a vector the \code{names} are taken from
  the \code{dimnames} (if any).  If the result is a length-one vector,
  the names are taken from the first dimension with a dimname.


> drop(matrix(1,1,1,dimnames=list("a","x")))
[1] 1
> drop(matrix(1,1,1,dimnames=list("a",NULL)))
> drop(matrix(1,1,1,dimnames=list(NULL,"x")))

so it actually only happens if only one dimension has a dimname.

However, when there is ambiguity, it seems quite reasonable to return an object with no names at all. Changing something as basic as this will break code left, right, and center, so is not likely to happen.
Comment 2 Martin Maechler 2018-08-08 10:53:24 UTC
let the "WONTFIX" mean  "won't fix probably"