Pivot longer assigns 1 column for 2 different binary variables

Hi I have imputed my data in wide format and I would like to convert my imputed data to long format.

This is my data

I am using this code to convert my imputed data to long format

imp_comp <-complete(imp1, action="long", include = TRUE) %>%
pivot_longer( cols = 3:16,
names_to = c(".value", "wave"),
names_pattern = "(.)_(.)")
The problems is that funnily R is trying to combine columns 10-14 (veg24_1, veg24_2, fruit24_1, fruit24_2) in one column named 4 instead of 2 columns (veg24, fruit24)

How can I assign columns 10-14 to 2 columns? Thanks in advance.

Hello,
I'm sure you shared this image with the best intentions, but perhaps you didnt realise what it implies.
If someone wished to use example data to test code against, they would type it out from your screenshot...

This is very unlikely to happen, and so it reduces the likelihood you will receive the help you desire.
Therefore please see this guide on how to reprex data. Key to this is use of either datapasta, or dput() to share your data as code

It is widely available data.

ok, if that's your genuine response and not an attempt at humour, I have no choice but to wish you luck and walk away slowly...

Good luck !

Sorry. I did not have any attempt to offend you. I am brand new to R and I am trying to learn.

I'm not offended, I'm just stumped.

Are you going to provide clues to the forum as to how to access this widely available data ?

Hi. I misunderstood your point completely. Here is the data and code

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 = "(.)_(.)")

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)

Thanks a lot for your response. Not exactly what I am looking for. What I am aiming for is to apply fixed effects modelling where maths scores, vegetable and fruit consumption are clustered within participant children over time. In this way I can explore whether changes in fruit and vegetable consumption with changes in maths scores over time.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.