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!