Search code examples
rnested-tibble

How to match elements with a nested vector colum?


I am trying to see how the elements in nested vector match with the Order ID from Order_ID column and I'm not quite sure how to match them together. For example:

enter image description here

I would like to create code to match the Order ID with the separate element in the nested vector. For example, I hope to see

enter image description here

I have tried using unlist() to extract the elements out from the nested vector but I'm still not sure how to match them with the Order ID. Is there any way to solve this problem? Any thoughts are appreciated.


Solution

  • Here you have a reproducible example of your issue:

    library(dplyr)
    
    df <- tibble(x = letters[1:3],
                 a = list(1L, 2:3, 4:6),
                 b = list(LETTERS[1], LETTERS[2:3], LETTERS[4:6]))
    
    df
    #> # A tibble: 3 x 3
    #>   x     a         b        
    #>   <chr> <list>    <list>   
    #> 1 a     <int [1]> <chr [1]>
    #> 2 b     <int [2]> <chr [2]>
    #> 3 c     <int [3]> <chr [3]>
    
    View(df)
    

    enter image description here

    And here how you can solve it:

    df %>% 
     group_by(x) %>% 
     summarise(across(c(a,b), unlist), .groups = "drop")
    #> # A tibble: 6 x 3
    #>   x         a b    
    #>   <chr> <int> <chr>
    #> 1 a         1 A    
    #> 2 b         2 B    
    #> 3 b         3 C    
    #> 4 c         4 D    
    #> 5 c         5 E    
    #> 6 c         6 F
    

    You need to set in group_by all the variables that you want to keep and be replicated (in this case x) and in across all the variables that you want to unlist (in this case c(a, b)).

    across will apply unlist to each column, by keeping in consideration the groups.

    I believe in your case the code should be:

    df %>% 
     group_by(Order_ID) %>% 
     summarise(across(c(Product, Price, Quantity), unlist), .groups = "drop")