HenrikBengtsson / Wishlist-for-R

Features and tweaks to R that I and others would love to see - feel free to add yours!
https://github.com/HenrikBengtsson/Wishlist-for-R/issues
GNU Lesser General Public License v3.0
133 stars 4 forks source link

Consistency: fix matrix subsetting behaviour to be consistent with vectors and data.frames. #148

Open karoliskoncevicius opened 1 year ago

karoliskoncevicius commented 1 year ago

There are a few corners where matrices are treated differently from vectors and data.frames. I will demonstrate the examples I have on a few simple objects:

x  <- setNames(1:5, letters[1:5])  # vector 

X  <- data.matrix(iris[1:10,])  # matrix
dimnames(X) <- list(letters[1:10], LETTERS[1:5])

df <- iris[1:10,]  # data.frame
dimnames(df) <- list(letters[1:10], LETTERS[1:5])

1. Selection of non existent elements should return NA:

x[20]
<NA> 
  NA

df[20,]
    A  B  C  D    E
NA NA NA NA NA <NA>

X[20,]
Error in X[20, ] : subscript out of bounds

2. having "NA" in selection should be allowed:

x[c("a", NA, "c")]
   a <NA>    c 
   1   NA    3 

df[c("a", NA, "c"),]
     A   B   C   D      E
a  5.1 3.5 1.4 0.2 setosa
NA  NA  NA  NA  NA   <NA>
c  4.7 3.2 1.3 0.2 setosa

X[c("a", NA, "c"),]
Error in X[c("a", NA, "c"), ] : subscript out of bounds

3. row and column names should not have names themselves

names(x) <- setNames(letters[1:5], LETTERS[1:5])
names(x)
[1] "a" "b" "c" "d" "e"

rownames(df) <- setNames(letters[1:10], LETTERS[1:10])
rownames(df)
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

rownames(X) <- setNames(letters[1:10], LETTERS[1:10])
rownames(X)
  A   B   C   D   E   F   G   H   I   J 
"a" "b" "c" "d" "e" "f" "g" "h" "i" "j"