'Pivot longer assigns 1 column for 2 different imputed binary variables

Hi I want to convert my multiple imputed data from wide to long format. Ideally after reshaping the data I would have 2 columns for veg and fruit. Instead R tries to combine both variables in 1 column. Your help would be much appreciated.

data.frame(
        .imp = c(0L, 0L, 0L, 0L, 0L),
         .id = c(1L, 2L, 3L, 4L, 5L),
     maths_1 = c(8, 8, 8, 8, 8),
     maths_2 = c(20, 5, 13, 16, 22),
   fruit24_1 = as.factor(c("No", "No", "No", "No", "Yes")),
   fruit24_2 = as.factor(c("No", "No", "Yes", "Yes", "Yes")),
     veg24_1 = as.factor(c("Yes", "Yes", "Yes", "No", "Yes")),
     veg24_2 = as.factor(c("Yes", "Yes", "Yes", "Yes", "Yes"))
) %>%
  pivot_longer( cols = 3:8,
                names_to = c(".value", "wave"),
                names_pattern = "(.)_(.)")


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source