rdataframematrixsubsettibble

Can we retrieve entries from a tibble using index matrix?


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 sm is 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? All I came up so far is as.data.frame(df_tibble)[sm]. So does it mean we can not subset a tibble with a indices matrix but have to transfer to a data.frame first?


Solution

  • If you are really intent on not converting tib to a plain data frame then any of these one-liners will do it

    purrr::map2_chr(sm[,1], sm[,2], ~ tib[[...]])
    ## [1] ] "i" "D" "g"
    
    mapply(\(...) tib[[...]], sm[,1], sm[,2])
    ## [1] "i" "D" "g"
    
    mapply(`[[`, list(tib), sm[,1], sm[,2])
    ## [1] "i" "D" "g"
    

    Note

    Input from question

    library(tibble)
    
    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)
    tib <- tibble(df)