Thanks for the example data.
Can you be more descriptive about the goal to achieve here ?
I understand going from wide to long, but what form should the long take.
It wont be possible to go long on all 3:8 columns as name/value pair, and remain true to the data types as math values are numeric whereas fruit and veg are factors.
I can go longwise on the data as if they were independent sets.
d1 <- 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"))
)
library(tidyverse)
core <- c(".imp",".id")
# fruit and veg parts
d1 %>% select(all_of(core) , where(is.factor)) %>%
pivot_longer(cols = !core)
#math parts
d1 %>% select(all_of(core) , where(is.numeric)) %>%
pivot_longer(cols = !core)