1

I have the following dataframe:

> input
  Year Variable Value
1 1950   Class1     1
2 1951   Class1     2
3 1952   Class1     3
4 1950   Class2     4
5 1951   Class2     5
6 1952   Class2     6
7 1950   Class3     7
8 1951   Class3     8
9 1952   Class3     9

I would like to create a wider dataframe that retains the "Year" column, removes the "Variable" and "Value" columns and adds columns called "Class1", "Class2" and "Class3", with values drawn from the "Value" column. The output would look like:

> output
  Year Class1 Class2 Class3
1 1950      1      4      7
2 1951      2      5      8
3 1952      3      6      9

I assume there is some way to use pivot_wider to do this, but I can't figure it out. Thanks!

shbrainard
  • 377
  • 2
  • 9

0 Answers0