Search code examples
rtidyr

tidyr::unnest() tibble, integer and vector


Wondering how to unnest the following:

test
# A tibble: 2 × 4
  site  data               samp dates      
  <chr> <list>            <int> <list>     
1 ARK   <tibble [15 × 2]>    15 <date [15]>
2 BCH   <tibble [44 × 2]>    44 <date [44]>

in the form of a (15 + 44) * 5 tibble with the columns site, alcohol (from data), opioids (from data), samp and dates.

test <- structure(list(site = c("ARK", "BCH"), data = list(structure(list(
    alcohol = c("Yes", "No", "Yes", "No", "No", "No", "Yes", 
    "No", "No", "No", "No", "No", "No", "No", "No"), opioids = c("No", 
    "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
    "No", "No", "No", "No")), row.names = c(NA, -15L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(alcohol = c("No", "No", 
"No", "Yes", "No", "No", "No", "Yes", "No", "No", "No", "No", 
"No", "No", "No", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "Yes", "No", "No", "Yes", "No", "Yes", "Yes", 
NA), opioids = c("No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No", "No", "No", 
"No", "No", NA)), row.names = c(NA, -44L), class = c("tbl_df", 
"tbl", "data.frame"))), samp = c(15L, 44L), dates = list(structure(c(19786, 
19854.0714285714, 19922.1428571429, 19990.2142857143, 20058.2857142857, 
20126.3571428571, 20194.4285714286, 20262.5, 20330.5714285714, 
20398.6428571429, 20466.7142857143, 20534.7857142857, 20602.8571428571, 
20670.9285714286, 20739), class = "Date"), structure(c(19786, 
19808.1627906977, 19830.3255813953, 19852.488372093, 19874.6511627907, 
19896.8139534884, 19918.976744186, 19941.1395348837, 19963.3023255814, 
19985.4651162791, 20007.6279069767, 20029.7906976744, 20051.9534883721, 
20074.1162790698, 20096.2790697674, 20118.4418604651, 20140.6046511628, 
20162.7674418605, 20184.9302325581, 20207.0930232558, 20229.2558139535, 
20251.4186046512, 20273.5813953488, 20295.7441860465, 20317.9069767442, 
20340.0697674419, 20362.2325581395, 20384.3953488372, 20406.5581395349, 
20428.7209302326, 20450.8837209302, 20473.0465116279, 20495.2093023256, 
20517.3720930233, 20539.5348837209, 20561.6976744186, 20583.8604651163, 
20606.023255814, 20628.1860465116, 20650.3488372093, 20672.511627907, 
20694.6744186047, 20716.8372093023, 20739), class = "Date"))), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame"))

Solution

  • Just pass on a vector of column names in tidyr::unnest()

    library(tidyverse)
    
    test %>% 
      unnest(c(data, dates))
    
    #> # A tibble: 59 × 5
    #>    site  alcohol opioids  samp dates     
    #>    <chr> <chr>   <chr>   <int> <date>    
    #>  1 ARK   Yes     No         15 2024-03-04
    #>  2 ARK   No      No         15 2024-05-11
    #>  3 ARK   Yes     No         15 2024-07-18
    #>  4 ARK   No      No         15 2024-09-24
    #>  5 ARK   No      No         15 2024-12-01
    #>  6 ARK   No      No         15 2025-02-07
    #>  7 ARK   Yes     No         15 2025-04-16
    #>  8 ARK   No      No         15 2025-06-23
    #>  9 ARK   No      No         15 2025-08-30
    #> 10 ARK   No      No         15 2025-11-06
    #> # ℹ 49 more rows
    

    Created on 2024-03-05 with reprex v2.0.2