Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
karoliskoncevicius opened this issue Jul 6, 2023 · 0 comments

Comments

@karoliskoncevicius
Copy link

karoliskoncevicius commented Jul 6, 2023

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" 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants