Transforming Survey Data Layout

Data Example.pdf (50.6 KB)

I need help transforming a dataset layout. I have an example in the attached PDF.

I've been trying all sorts of stuff with lapply() and split() with no luck. Anyone have the solution?

Thanks!

You can use tidyr::pivot_wider()

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

This snippet should get you going.

library(tidyverse)
current <- data.frame(
  question = c('q1', 'q1', 'q2', 'q2'),
  last = c('a', 'c', 'a', 'c'),
  first = c('b', 'd', 'b', 'd'),
  answer = c(1,2,3,4)
)

new <- current %>%
  pivot_wider(
    id_cols = 2:3,
    names_from = question,
    values_from = answer
  )
1 Like

Worked perfectly. Thank you for taking the time to write the actual script. Helps me learn by example

This topic was automatically closed 7 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.