Hello, I have stumbled upon a few cases where the behaviour of naming and subsetting in matrices seems unintuitive. All those look related so wanted to put everything in one message.
1. Why row/col selection by names with NAs is not allowed? x <- setNames(1:10, letters[1:10]) X <- matrix(x, nrow=2, dimnames = list(letters[1:2], LETTERS[1:5])) x[c(1, NA, 3)] # vector: works and adds "NA" x[c("a", NA, "c")] # vector: works and adds "NA" X[,c(1, NA, 3)] # works and selects "NA" column X[,c("A", NA, "C")] # <error> 2. Should setting names() for a matrix be allowed? names(X) <- paste0("e", 1:length(X)) X["e4"] # works # but any operation on a matrix drops the names X <- X[,-1] # all names are gone X["e4"] # <error> Maybe names() should not be allowed on a matrix? 3. Should selection of non-existent dimension names really be an error? x[22] # works on a vector - gives "NA" X[,22] # <error> A potential useful use-case is matching a smaller matrix to a larger one: A <- matrix(rnorm(10), nrow=2, dimnames = list(c("a","c"))) B <- matrix(rnorm(20), nrow=4, dimnames = list(c("a", "b", "c", "d"))) # matching larger matrix to the smaller one <works> B[rownames(A),] # matching smaller matrix to the larger one <error> A[rownames(B),] These also doesn't seem to be documented in '[', 'names', 'rownames’. Interested if there specific reasons for this behaviour, or could these potentially be adjusted? Kind regards, Karolis K. ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel