0

I have this dataset:

     var1_19Q1  var2_19Q1  var1_19Q2  var2_19Q2  var1_19Q3  var2_19Q3
1       2.8        3.6        2.2        1.5        1.2        2.3

I want to reshape it in order to have:

      var1 var2
19Q1   2.8  3.6
19Q2   2.2  1.5
19Q3   1.2  2.3

Do you know how to do it?

Thanks

R

  • `tidyr::pivot_longer(df, cols = starts_with('var'), names_to = c('.value', 'col'), names_sep = "_")` – Ronak Shah Aug 28 '20 at 10:34
  • As the table above is a simplified example, is there a way to do it even if there are much more variables than var1 and var2 and their names are different in structure? For instance: gdp_19Q1 gdp_19Q2 gdp_19Q3 infl_19Q1 infl_19Q2 infl_19Q3 int_rat_19q1 and so on and so forth – Raschid Rasko Aug 28 '20 at 10:47

0 Answers0