rcombn

How to apply the combn() function to a column of list type?


I have a dataframe with 3 columns, one of which is of list type

>head(basket_data)
# A tibble: 8 x 3
  order_id items      count
     <int> <list>     <int>
1        2 <chr [9]>      9
2        3 <chr [8]>      8
3        4 <chr [13]>    13
4        5 <chr [26]>    26
5        6 <chr [3]>      3

Say for row 1 (order_id 2) the list is [a,b,c]

I want to create a new column using the combn function (maybe?) which will have all pair combinations of the list for that row only, so [[a,b],[b,c],[a,c]]

All the examples of using combn that I have come across on SO have combined every list in the dataframe and paired the entire lists up. Any help would be appreciated. Thanks!


Solution

  • Let's say your data is something like this

    test <- structure(list(items = list(c('a', 'b'), c('b', 'c', 'd'), c('d', 'e'), c('f', 'g', 'i'), c('g', 'h')), 
                   ID = 1:5), row.names = c(NA, 5L), class = "data.frame")
    
    test
        items ID
    1    a, b  1
    2 b, c, d  2
    3    d, e  3
    4 f, g, i  4
    5    g, h  5
    
    as_tibble(test)
    # A tibble: 5 x 2
      items        ID
      <list>    <int>
    1 <chr [2]>     1
    2 <chr [3]>     2
    3 <chr [2]>     3
    4 <chr [3]>     4
    5 <chr [2]>     5
    

    Then you may do something like this

    as_tibble(test) %>% mutate(combs = map(items, ~combn(.x, 2)))
    
    # A tibble: 5 x 3
      items        ID combs            
      <list>    <int> <list>           
    1 <chr [2]>     1 <chr[,1] [2 x 1]>
    2 <chr [3]>     2 <chr[,3] [2 x 3]>
    3 <chr [2]>     3 <chr[,1] [2 x 1]>
    4 <chr [3]>     4 <chr[,3] [2 x 3]>
    5 <chr [2]>     5 <chr[,1] [2 x 1]>
    

    Check

    as_tibble(test) %>% mutate(combs = map(items, ~combn(.x, 2))) %>%
      data.frame()
        items ID            combs
    1    a, b  1             a, b
    2 b, c, d  2 b, c, b, d, c, d
    3    d, e  3             d, e
    4 f, g, i  4 f, g, f, i, g, i
    5    g, h  5             g, h
    

    OR

    as_tibble(test) %>% mutate(combs = map(items, ~combn(.x, 2, list)))
    
    # A tibble: 5 x 3
      items        ID combs     
      <list>    <int> <list>    
    1 <chr [2]>     1 <list [1]>
    2 <chr [3]>     2 <list [3]>
    3 <chr [2]>     3 <list [1]>
    4 <chr [3]>     4 <list [3]>
    5 <chr [2]>     5 <list [1]>
    

    depends on input and output format you want