I have a matrix where the row and column indices are stored that I want to retrieve from a dataset. With a data.frame
this works fine:
set.seed(1)
df <- data.frame(a= letters[1:10], b= LETTERS[1:10])
sm <- matrix(c(sample(1:10, 3, replace= TRUE), sample(1:2, 3, replace= TRUE)), ncol= 2)
df[sm]
[1] "i" "D" "g"
But using the same indices matrix to get entries from a tibble
fails:
df_tibble <- tibble::as_tibble(df)
df_tibble[sm]
This returns the error Error in
df_tibble[sm]: ! Subscript
smis a matrix, it must be of type logical. Run
rlang::last_trace() to see where the error occurred.
How to use sm
to get the entries df[sm]
from df_tibble
?