0

I have a wide-format dataset that I would like to tidy using pivot_longer. The dataset consists of one column for IDs, three longitudinal measurements of the variable X each as columns (X1, X2, X3), and three longitudinal measurements of the variable Y each as columns (Y1, Y2, Y3). I'd like to have a long-format dataset that is simply ID, month, X and Y.

I tried to include all the variables X1, X2, X3, Y1, Y2, Y3 in the cols argument of pivot_longer function, and created two names (X, Y) for the names_to argument, but that does not work.

Data that I have:

Data_Have <- tribble(~ID, ~x1, ~x2, ~x3, ~y1, ~y2, ~y3,
                     "A", 3, 3, 3, 2.4, 2.5, 2.3,
                     "B", 3, 1, 2, 1.5, 1.6, 1.7)

Data that I want:

Data_Want <- tribble(~ID, ~month, ~x, ~y,
                     "A", 1, 3, 2.4,
                     "A", 2, 3, 2.5,
                     "A", 3, 3, 2.3,
                     "B", 1, 3, 1.5,
                     "B", 2, 1, 1.6,
                     "B", 3, 2, 1.7)

Thank you!

Pharmepi
  • 21
  • 2
  • 2
    Try `Data_Have %>% tidyr::pivot_longer(-ID, names_pattern="(\\w+)(\\d+)", names_to=c(".value","month"))` – MrFlick Jun 08 '23 at 19:51
  • 2
    This is nearly identical to the last example at the `?pivot_longer` help page - I'd suggest running through all 4 examples to understand the function better. The code in the last example: `anscombe %>% pivot_longer(everything(), cols_vary = "slowest", names_to = c(".value", "set"), names_pattern = "(.)(.)")`, code that works here: `Data_Have |> pivot_longer(-ID, names_pattern = "(.)(.)", names_to = c(".value", "month"))` – Gregor Thomas Jun 08 '23 at 19:53

0 Answers0