0

I want to re-organize a longitudinal dataset in R. The original dataset looks like the one below.

ID y1_demo y2_demo y1_ex y2_ex
100 A B A B
200 B A A C

I would like to see the output like the following.

ID Year demo ex
100 Y1 A A
100 Y2 B B
200 Y1 B A
200 Y2 A C
Katie
  • 19
  • 2
  • You may use `tidyr::pivot_longer(df, cols = -ID, names_to = c('Year', '.value'), names_sep = '_')` where `df` is your dataset name. – Ronak Shah Dec 16 '22 at 01:35
  • Another option: `pivot_longer(df,cols = -ID, names_pattern = "^(.*?)_demo", names_to = "Year", names_transform = list(Year = toupper), values_to = "Demo")` – Vinícius Félix Dec 16 '22 at 01:42

0 Answers0