Home > Software engineering >  Follow-up: Turning a long-format dataframe into a one-row wide-format dataframe
Follow-up: Turning a long-format dataframe into a one-row wide-format dataframe

Time:12-17

I'm following up on this great answer. I was wondering if there is a way to wide-format my long_data to achieve my desired_output below?

I tried:

pivot_wider(long_data, names_from = c(DV, time),values_from = c(score, task_type, mot), names_glue = "{.value}_{DV}_T{time}")

without success.

library(tidyverse)
m="
class sub  order time DV score task_type mot
1     1    s-c   1    ac 1     simple    1.5
1     1    s-c   1    bc 2     simple    1.5
1     1    s-c   2    ac 3     complex   0.2
1     1    s-c   2    bc 4     complex   0.2
"

long_data = read.table(text = m, header = TRUE)
desired_output =
  "
class sub  order DV_ac_T1 DV_ac_T2 DV_bc_T1 DV_bc_T2 task_type_T1  task_type_T2 mot_T1  mot_T2
1     1    s-c   1        3        2        4        simple        complex      1.5     0.2
"

CodePudding user response:

I think what makes this a little tricky is that you have one variable (score) which varies by both DV and time, and two variables (task_type mot) which vary by time but not by DV. One way to handle that would be to pivot the two separately and join the results:

library(tidyverse)
left_join(
  select(long_data, -task_type, -mot) %>%
    pivot_wider(names_from = c(DV, time), names_glue = "DV_{DV}_T{time}", 
               values_from = score),
  distinct(long_data, class, sub, order,time, task_type, mot) %>%
    pivot_wider(names_from = time, values_from = c(task_type, mot))
)

Result

Joining with `by = join_by(class, sub, order)`
# A tibble: 1 × 11
  class   sub order DV_ac_T1 DV_bc_T1 DV_ac_T2 DV_bc_T2 task_type_1 task_type_2 mot_1 mot_2
  <int> <int> <chr>    <int>    <int>    <int>    <int> <chr>       <chr>       <dbl> <dbl>
1     1     1 s-c          1        2        3        4 simple      complex       1.5   0.2

There's probably a more concise way using pivot_wider_spec, but I prefer separating the problem in two and chunking the two pieces together.

  • Related