Bug 16297 - one.column.matrix[one.row, ] has no names
Summary: one.column.matrix[one.row, ] has no names
Status: NEW
Alias: None
Product: R
Classification: Unclassified
Component: Misc (show other bugs)
Version: R-devel (trunk)
Hardware: All Linux
: P5 minor
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2015-04-02 20:17 UTC by Toby Dylan Hocking
Modified: 2015-04-02 22:03 UTC (History)
1 user (show)

See Also:


Attachments

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")))
  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.

but

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

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.