Is there an alternative function for unnest_longer()?

71 Views Asked by At

found a solution: https://stackoverflow.com/a/59192970/23061034

Is there any function that works like unnest_longer()? I am trying to run the following lines of codes but an error keeps occurring when trying to unnest.

df %>%
  filter(n() > 2L, .by = sn2) %>%
  pivot_wider(names_from = sn3, values_from = dml1_5:leisure, 
              values_fn = list) %>%
  unnest_longer(dml1_5_1:leisure_2, keep_empty = TRUE) 

Error in unnest_longer(): ! In row 9, can't recycle input of size 4 to size 2.

Desired output:

sn2 leisure.1 hmonth.1 hyear.1 leisure.2 hmonth.2 hyear.2
227 230 6 2000 540 6 2000
227 130 6 2000 170 6 2000
250 370 6 2000 380 6 2000
250 380 6 2000 190 6 2000
294 120 6 2000 210 6 2000
294 200 6 2000 310 6 2000
307 130 7 2000 220 7 2000
307 480 7 2000 270 7 2000

Data:

dput(df)
structure(list(sn2 = structure(c(227, 227, 227, 227, 249, 249, 
250, 250, 250, 250, 294, 294, 294, 294, 307, 307, 307, 307, 318, 
318, 341, 341, 352, 352, 352, 352, 363, 363, 385, 385, 385, 385, 
409, 409, 455, 455, 455, 455, 535, 535, 535, 535, 568, 568, 580, 
580, 591, 591, 591, 591, 661, 661, 683, 683, 718, 718, 741, 741, 
741, 741, 763, 763, 763, 763, 809, 809, 809, 809, 26, 26, 26, 
26, 37, 37, 37, 37, 93, 93, 117, 117, 117, 139, 139, 151, 151, 
162, 162, 162, 162, 210, 210, 210, 210, 221, 221, 221, 221, 254, 
254, 254, 254, 265, 265, 265, 265, 301, 301, 301, 301, 345, 345, 
356, 356, 356, 356, 390, 390, 390, 390, 517, 517, 562, 562, 573, 
573, 573, 573, 595, 595, 595, 595, 610, 610, 621, 621, 621, 621, 
632, 632, 643, 643, 712, 712, 712, 712, 723, 723, 723, 723, 745, 
745, 745, 745, 789, 789, 19, 19, 19, 19, 20, 20, 20, 20, 42, 
42, 75, 75, 86, 86, 86, 86, 100, 100, 100, 100, 155, 155, 155, 
155, 166, 166, 166, 166, 214, 214, 225, 225, 225, 225, 247, 247, 
247, 247, 258, 258, 281, 281, 316, 316, 316, 316, 327, 327, 338, 
338, 361, 361, 383, 383, 394, 394, 394, 394, 422, 422, 422, 422, 
433, 433, 433, 433, 444, 444, 444, 444, 466, 466, 499, 499, 499, 
499, 524, 524, 524, 524, 568, 568, 568, 568, 580, 580, 580, 580, 
591, 591, 591, 591, 649, 649, 649, 649, 672, 672, 672, 672, 683, 
683, 683, 683, 707, 707, 707, 707, 730, 730, 730, 730, 741, 741, 
741, 741, 785, 785, 785, 785, 809, 809, 809, 809, 59, 59, 59, 
59, 82, 82, 82, 82, 106, 106, 106, 106, 117, 117, 117, 117, 128, 
128, 151, 151, 151, 151, 162, 162, 162, 162, 173, 173, 173, 173, 
184, 184, 184, 184, 221, 221, 232, 232, 232, 232, 254, 254, 254, 
254, 265, 265, 276, 276, 298, 298, 298, 298, 345, 345, 345, 345, 
367, 367, 367, 367, 389, 389, 389, 389, 528, 528, 528, 528, 539, 
539, 539, 539, 551, 551, 584, 584), label = "household number", format.stata = "%8.0g"), 
    sn3 = structure(c(1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1, 2, 
    2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 
    1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 
    1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 
    2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 
    1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 
    2, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 
    1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 
    1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 
    1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 
    1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 
    1, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 
    2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 
    1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 
    1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 
    1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 
    2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 
    1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1), label = "person number", format.stata = "%8.0g"), 
    dml1_5 = structure(c(70, 30, 170, 30, 180, 250, 230, 210, 
    170, 30, 80, 110, 10, 40, 30, 420, 20, 270, 170, 160, 60, 
    70, 90, 10, 380, 20, 360, 170, 70, 120, 190, 110, 60, 230, 
    0, 0, 0, 130, 10, 70, 590, 160, 220, 260, 140, 30, 0, 0, 
    0, 0, 70, 110, 60, 0, 70, 290, 80, 0, 70, 140, 140, 150, 
    360, 380, 20, 90, 30, 30, 40, 100, 110, 110, 70, 100, 0, 
    70, 110, 70, 50, 170, 0, 610, 240, 160, 250, 70, 170, 70, 
    200, 0, 0, 20, 60, 150, 10, 10, 30, 250, 230, 150, 0, 90, 
    100, 0, 10, 270, 0, 330, 60, 0, 40, 0, 0, 50, 30, 0, 10, 
    60, 30, 0, 30, 70, 230, 0, 0, 0, 0, 150, 0, 420, 140, 50, 
    140, 40, 70, 10, 30, 0, 360, 50, 660, 180, 190, 100, 120, 
    110, 160, 110, 210, 40, 30, 0, 90, 10, 0, 170, 0, 310, 10, 
    270, 30, 270, 40, 10, 0, 80, 90, 0, 80, 60, 70, 250, 250, 
    80, 170, 60, 220, 210, 240, 0, 0, 110, 0, 330, 30, 40, 10, 
    60, 0, 90, 50, 130, 140, 90, 170, 60, 290, 80, 0, 0, 0, 180, 
    60, 0, 40, 0, 130, 110, 130, 0, 0, 40, 80, 0, 0, 10, 0, 80, 
    210, 100, 90, 30, 70, 60, 30, 60, 10, 0, 20, 10, 10, 0, 0, 
    0, 170, 220, 50, 40, 0, 0, 180, 30, 350, 90, 30, 90, 0, 0, 
    110, 0, 0, 30, 0, 0, 0, 270, 80, 430, 10, 130, 100, 0, 20, 
    130, 60, 0, 400, 50, 0, 10, 70, 120, 220, 160, 240, 70, 0, 
    120, 0, 0, 300, 70, 210, 20, 0, 0, 0, 210, 20, 60, 0, 30, 
    300, 0, 0, 40, 140, 0, 40, 0, 80, 40, 0, 0, 0, 60, 310, 60, 
    350, 190, 0, 40, 60, 60, 70, 30, 0, 180, 30, 100, 30, 80, 
    40, 30, 70, 0, 140, 20, 10, 50, 0, 230, 120, 90, 150, 30, 
    280, 10, 310, 0, 10, 0, 0, 90, 0, 300, 0, 0, 0, 0, 130, 20, 
    70, 180, 30), label = "main activity - social life & entertainment (mins per day)", format.stata = "%8.0g"), 
    dml1_6 = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 0, 0, 370, 0, 0, 0, 160, 
    0, 100, 0, 0, 190, 0, 130, 0, 200, 0, 0, 30, 0, 0, 20, 0, 
    0, 0, 0, 0, 0, 0, 90, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 70, 0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 0, 60, 0, 0, 0, 0, 
    50, 140, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 
    40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 30, 50, 0, 60, 0, 0, 0, 0, 60, 0, 0, 60, 
    0, 70, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 
    0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 70, 0, 0, 0, 0, 150, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 110, 0, 0, 80, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 230, 540, 100, 0, 
    90, 0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 70, 0, 0, 
    0, 0, 0, 180, 0, 180, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 170, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 60, 0, 0, 0, 0, 0, 0), label = "main activity - sports & outdoor activities (mins per day)", format.stata = "%8.0g"), 
    dml1_7 = structure(c(0, 0, 0, 0, 210, 0, 0, 0, 0, 0, 0, 0, 
    0, 20, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 130, 
    0, 0, 0, 0, 0, 390, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 90, 0, 0, 0, 0, 20, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 
    0, 20, 0, 0, 230, 0, 0, 80, 0, 0, 0, 0, 0, 0, 0, 170, 10, 
    10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 30, 0, 0, 0, 
    0, 0, 0, 0, 20, 0, 0, 30, 30, 0, 0, 30, 0, 0, 0, 0, 30, 30, 
    20, 0, 0, 180, 0, 240, 0, 0, 180, 0, 0, 180, 30, 40, 20, 
    20, 0, 10, 40, 0, 0, 190, 180, 150, 30, 60, 0, 0, 0, 20, 
    10, 0, 10, 0, 0, 120, 70, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 20, 0, 80, 
    0, 0, 0, 60, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 10, 10, 110, 110, 120, 10, 120, 70, 50, 0, 0, 0, 0, 
    10, 10, 0, 0, 90, 190, 0, 10, 0, 0, 0, 0, 0, 0, 0, 120, 10, 
    420, 250, 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 10, 0, 40, 0, 0, 40, 60, 0, 0, 0, 0, 0, 60, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 140, 0, 0, 
    140, 0, 170, 790, 0, 0, 0, 0, 0, 0, 60, 30, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 70, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 50, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 330, 40), label = "main activity - hobbies & games (mins per day)", format.stata = "%8.0g"), 
    dml1_8 = structure(c(160, 100, 370, 140, 40, 230, 140, 170, 
    210, 130, 40, 90, 200, 250, 100, 60, 200, 0, 170, 60, 10, 
    250, 80, 130, 240, 0, 150, 290, 70, 40, 70, 0, 20, 0, 370, 
    230, 350, 0, 150, 50, 50, 240, 270, 460, 150, 180, 50, 60, 
    10, 20, 300, 270, 120, 80, 170, 60, 10, 40, 60, 20, 140, 
    90, 260, 0, 260, 100, 140, 40, 20, 120, 100, 240, 10, 300, 
    220, 140, 220, 140, 10, 100, 250, 130, 150, 280, 170, 320, 
    240, 190, 180, 80, 100, 90, 100, 40, 170, 30, 270, 210, 0, 
    150, 120, 130, 260, 140, 340, 120, 170, 50, 320, 110, 110, 
    250, 230, 370, 180, 120, 80, 130, 190, 420, 500, 30, 10, 
    110, 310, 0, 230, 500, 80, 180, 240, 350, 140, 80, 100, 330, 
    360, 100, 150, 150, 100, 100, 70, 270, 270, 130, 140, 80, 
    230, 150, 200, 270, 620, 80, 10, 250, 210, 150, 420, 0, 120, 
    0, 290, 230, 340, 150, 60, 180, 230, 170, 180, 200, 10, 510, 
    250, 30, 0, 240, 80, 0, 0, 120, 100, 0, 0, 310, 230, 320, 
    200, 390, 410, 310, 320, 170, 220, 140, 90, 0, 160, 0, 150, 
    280, 180, 370, 310, 130, 90, 380, 300, 210, 280, 200, 70, 
    0, 60, 160, 230, 50, 120, 150, 120, 120, 150, 170, 400, 310, 
    470, 80, 70, 280, 170, 400, 480, 560, 510, 10, 50, 80, 90, 
    190, 250, 160, 110, 80, 90, 20, 10, 90, 140, 140, 50, 90, 
    70, 170, 140, 80, 180, 80, 210, 150, 220, 280, 140, 0, 210, 
    250, 0, 0, 560, 170, 150, 0, 0, 0, 0, 0, 0, 150, 0, 90, 30, 
    160, 30, 50, 160, 180, 130, 330, 140, 670, 250, 100, 240, 
    180, 40, 140, 290, 150, 320, 90, 350, 30, 80, 260, 240, 120, 
    180, 280, 300, 0, 100, 80, 30, 220, 330, 170, 20, 170, 50, 
    250, 110, 260, 110, 130, 310, 100, 160, 230, 150, 230, 130, 
    330, 160, 290, 280, 240, 190, 160, 180, 270, 410, 250, 260, 
    250, 420, 60, 0, 150, 200, 140, 170, 190, 170, 120, 480), label = "main activity - mass media (mins per day)", format.stata = "%8.0g"), 
    hmonth = structure(c(6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
    6, 6, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 6, 6, 6, 
    6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 
    9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 9, 9, 9, 9, 7, 
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 
    8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 11, 11, 11, 11, 5, 
    5, 5, 5, 7, 7, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
    7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 7, 7, 10, 10, 7, 7, 7, 
    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 
    6, 6, 6, 6, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
    6, 9, 9, 10, 10, 10, 10, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 
    9, 9, 9, 11, 11, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 
    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
    8, 8, 8, 8, 9, 9, 9, 9, 8, 8, 8, 8, 9, 9, 9, 9, 8, 8, 8, 
    8, 9, 9, 9, 9, 8, 8, 8, 8, 10, 10, 10, 10, 8, 8, 8, 8, 9, 
    9, 9, 9, 11, 11, 11, 11, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8, 8, 
    8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 
    9, 9, 9, 9, 10, 10, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 
    10, 10, 10, 10, 10, 10, 10, 10, 9, 9, 9, 9, 9, 9, 9, 9, 10, 
    10, 10, 10), label = "month of household interview", format.stata = "%8.0g", labels = c(january = 1, 
    february = 2, march = 3, april = 4, may = 5, june = 6, july = 7, 
    august = 8, september = 9, october = 10, november = 11, december = 12
    ), class = c("haven_labelled", "vctrs_vctr", "double")), 
    hyear = structure(c(2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2001, 2001, 
    2001, 2001, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2001, 2001, 
    2001, 2001, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
    2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000), label = "year of household interview", format.stata = "%8.0g"), 
    leisure = c(230, 130, 540, 170, 430, 480, 370, 380, 380, 
    190, 120, 200, 210, 310, 130, 480, 220, 270, 340, 260, 90, 
    320, 170, 140, 620, 390, 510, 460, 140, 450, 260, 210, 80, 
    230, 560, 620, 480, 130, 360, 120, 640, 430, 490, 720, 310, 
    210, 50, 60, 10, 20, 460, 380, 270, 170, 240, 370, 90, 40, 
    130, 160, 300, 240, 620, 380, 280, 190, 170, 70, 150, 220, 
    210, 580, 110, 400, 300, 210, 330, 210, 60, 270, 250, 740, 
    560, 450, 430, 430, 410, 260, 380, 80, 100, 110, 160, 190, 
    180, 70, 310, 520, 260, 300, 120, 220, 410, 280, 400, 390, 
    190, 380, 380, 140, 180, 250, 230, 450, 210, 120, 90, 190, 
    250, 530, 590, 100, 240, 290, 310, 240, 230, 650, 260, 600, 
    380, 580, 310, 160, 190, 360, 390, 110, 550, 200, 760, 470, 
    440, 520, 420, 330, 350, 190, 500, 210, 240, 270, 720, 150, 
    10, 540, 340, 530, 500, 270, 150, 270, 330, 250, 340, 230, 
    150, 180, 310, 230, 250, 450, 260, 590, 420, 100, 220, 450, 
    320, 0, 0, 230, 100, 350, 120, 430, 240, 380, 200, 540, 460, 
    440, 480, 260, 390, 200, 380, 80, 160, 0, 150, 460, 240, 
    370, 350, 130, 220, 500, 440, 320, 390, 360, 160, 180, 130, 
    270, 230, 130, 330, 250, 220, 160, 220, 230, 590, 560, 480, 
    90, 90, 440, 180, 400, 480, 560, 680, 350, 110, 540, 340, 
    190, 430, 190, 460, 280, 140, 110, 90, 90, 250, 140, 50, 
    120, 70, 170, 140, 350, 260, 510, 230, 280, 360, 280, 160, 
    400, 870, 350, 400, 140, 560, 180, 280, 150, 220, 160, 240, 
    70, 0, 270, 0, 90, 330, 230, 310, 70, 160, 180, 130, 580, 
    480, 730, 430, 270, 540, 350, 830, 180, 430, 150, 360, 90, 
    430, 130, 110, 260, 240, 180, 490, 340, 650, 190, 100, 120, 
    90, 280, 400, 200, 20, 350, 80, 350, 210, 340, 150, 160, 
    380, 100, 470, 250, 160, 280, 130, 560, 280, 430, 440, 270, 
    470, 170, 490, 270, 420, 250, 260, 340, 420, 360, 0, 150, 
    260, 140, 300, 210, 240, 630, 550)), row.names = c(NA, -355L
), class = c("tbl_df", "tbl", "data.frame"), na.action = structure(c(`3492` = 3492L, 
`3493` = 3493L, `3494` = 3494L, `3495` = 3495L, `3496` = 3496L, 
`3497` = 3497L, `3498` = 3498L, `3499` = 3499L, `3500` = 3500L, 
`3501` = 3501L, `3508` = 3508L, `3509` = 3509L, `3510` = 3510L, 
`3511` = 3511L, `3512` = 3512L, `3513` = 3513L, `3518` = 3518L, 
`3519` = 3519L, `3520` = 3520L, `3521` = 3521L, `3522` = 3522L, 
`3523` = 3523L, `3524` = 3524L, `3525` = 3525L), class = "omit"))
2

There are 2 best solutions below

2
Andy Baxter On

Using your dataset above, adding keep_empty = TRUE works for me to unnest all rows:

library(tidyverse)

df %>%
  pivot_wider(names_from = sn3, values_from = leisure:hyear, 
              values_fn = list) %>%
  unnest_longer(leisure_1:hyear_2, keep_empty = TRUE) 

#> # A tibble: 353 × 10
#>      sn2 dml1_5 dml1_6 dml1_7 dml1_8  hmonth leisure_1 leisure_2 hyear_1 hyear_2
#>    <dbl>  <dbl>  <dbl>  <dbl>  <dbl> <hvn_l>     <dbl>     <dbl>   <dbl>   <dbl>
#>  1   227     70      0      0    160       6       230        NA    2000      NA
#>  2   227     30      0      0    100       6       130        NA    2000      NA
#>  3   227    170      0      0    370       6        NA       540      NA    2000
#>  4   227     30      0      0    140       6        NA       170      NA    2000
#>  5   249    180      0    210     40       6       430        NA    2000      NA
#>  6   249    250      0      0    230       6       480        NA    2000      NA
#>  7   250    230      0      0    140       6       370        NA    2000      NA
#>  8   250    210      0      0    170       6       380        NA    2000      NA
#>  9   250    170      0      0    210       6        NA       380      NA    2000
#> 10   250     30     30      0    130       6        NA       190      NA    2000
#> # ℹ 343 more rows
9
r2evans On

The problem happens when the number of values within any one cell (in a row) is not unambiguously-recyclable with the other columns to unnest.

As an example,

quux <- df %>%
  filter(n() > 2L, .by = sn2) %>%
  pivot_wider(names_from = sn3, values_from = leisure:hyear, 
              values_fn = list) %>%
  mutate(
    leisure_1 = c(list(rep(leisure_1[[1]], 3)), leisure_1[-1]),
    hyear_1 = c(list(rep(hyear_1[[1]], 2)), hyear_1[-1])
  ) %>%
  head()
quux
# # A tibble: 6 × 10
#     sn2 dml1_5 dml1_6 dml1_7 dml1_8 hmonth    leisure_1 leisure_2 hyear_1   hyear_2  
#   <dbl>  <dbl>  <dbl>  <dbl>  <dbl> <dbl+lbl> <list>    <list>    <list>    <list>   
# 1   227     70      0      0    160 6 [june]  <dbl [3]> <NULL>    <dbl [2]> <NULL>   
# 2   227     30      0      0    100 6 [june]  <dbl [1]> <NULL>    <dbl [1]> <NULL>   
# 3   227    170      0      0    370 6 [june]  <NULL>    <dbl [1]> <NULL>    <dbl [1]>
# 4   227     30      0      0    140 6 [june]  <NULL>    <dbl [1]> <NULL>    <dbl [1]>
# 5   250    230      0      0    140 6 [june]  <dbl [1]> <NULL>    <dbl [1]> <NULL>   
# 6   250    210      0      0    170 6 [june]  <dbl [1]> <NULL>    <dbl [1]> <NULL>   

unnest_longer(quux, leisure_1:hyear_2, keep_empty = TRUE) 
# Error in unnest_longer(quux, leisure_1:hyear_2, keep_empty = TRUE) : 
#   In row 1, can't recycle input of size 3 to size 2.

We can see that on row 1, leisure_1 has 3 values whereas hyear_1 has only 2. If one of them had only 1, then the tidyverse rules of recycling ("1 or n") would allow it to work without error. (In constrast, native R's recycling rules are sloppy, allowing length-4 and length-2 to be compatible since 4 is an even multiple of 2. This can be used in clever hacks, but in my experience more often than not results in incorrect expansions/recycling, and should always be avoided. I think others agree, since tidyverse recycling rules require that all values to be combined in this fashion must only be length-1 or the same length as the longest.)

Without knowing the true context of observational alignment of your data, we are able to impose some same-length-ness by extending the short column to be the length of the longer.

quux %>%
  mutate(
    len_1 = pmax(lengths(leisure_1), lengths(hyear_1)),
    len_2 = pmax(lengths(leisure_2), lengths(hyear_2)),
    across(c(leisure_1, hyear_1), ~ Map(`length<-`, .x, len_1)), 
    across(c(leisure_2, hyear_2), ~ Map(`length<-`, .x, len_2))
  ) %>%
  unnest_longer(leisure_1:hyear_2, keep_empty = TRUE)
# # A tibble: 8 × 12
#     sn2 dml1_5 dml1_6 dml1_7 dml1_8 hmonth    leisure_1 leisure_2 hyear_1 hyear_2 len_1 len_2
#   <dbl>  <dbl>  <dbl>  <dbl>  <dbl> <dbl+lbl>     <dbl>     <dbl>   <dbl>   <dbl> <int> <int>
# 1   227     70      0      0    160 6 [june]        230        NA    2000      NA     3     0
# 2   227     70      0      0    160 6 [june]        230        NA    2000      NA     3     0
# 3   227     70      0      0    160 6 [june]        230        NA      NA      NA     3     0
# 4   227     30      0      0    100 6 [june]        130        NA    2000      NA     1     0
# 5   227    170      0      0    370 6 [june]         NA       540      NA    2000     0     1
# 6   227     30      0      0    140 6 [june]         NA       170      NA    2000     0     1
# 7   250    230      0      0    140 6 [june]        370        NA    2000      NA     1     0
# 8   250    210      0      0    170 6 [june]        380        NA    2000      NA     1     0

Notice the change: I had artificially repeated the first leisure_1 value to be length-3, and the corresponding hyear_1 to be length-2. Looking at the first three rows, we have two rows where the leisure_1 and hyear_1 values correspond find, and the third row has an empty (NA) hyear_1.

Realize that this preserves all of the data, but might break some assumptions about a row being a complete observation.


Edit: if you want to drop the extras (that is, if we have 4 in one column and 2 in another, then we'll keep only the first 2 from the column-with-4), then

quux %>%
  mutate(
    len_1 = pmin(lengths(leisure_1), lengths(hyear_1)),
    len_2 = pmin(lengths(leisure_2), lengths(hyear_2)),
    across(c(leisure_1, hyear_1), ~ Map(head, .x, len_1)), 
    across(c(leisure_2, hyear_2), ~ Map(head, .x, len_2))
  ) %>%
  unnest_longer(leisure_1:hyear_2, keep_empty = TRUE)
# # A tibble: 7 × 12
#     sn2 dml1_5 dml1_6 dml1_7 dml1_8 hmonth    leisure_1 leisure_2 hyear_1 hyear_2 len_1 len_2
#   <dbl>  <dbl>  <dbl>  <dbl>  <dbl> <dbl+lbl>     <dbl>     <dbl>   <dbl>   <dbl> <int> <int>
# 1   227     70      0      0    160 6 [june]        230        NA    2000      NA     2     0
# 2   227     70      0      0    160 6 [june]        230        NA    2000      NA     2     0
# 3   227     30      0      0    100 6 [june]        130        NA    2000      NA     1     0
# 4   227    170      0      0    370 6 [june]         NA       540      NA    2000     0     1
# 5   227     30      0      0    140 6 [june]         NA       170      NA    2000     0     1
# 6   250    230      0      0    140 6 [june]        370        NA    2000      NA     1     0
# 7   250    210      0      0    170 6 [june]        380        NA    2000      NA     1     0