I am looking for a way to spread N rows into columns with R. Example input -> output
given a table, I want to spread that years rows into columns
id | year | val_1 | val_2 | val_3
---|------|-------|-------|------
1 | 2001 | 10 | 11 | 12
2 | 2001 | 21 | 22 | 22
3 | 2002 | 31 | 32 | 32
3 | 2002 | 41 | 42 | 42
to look like the following:
id | year | val_1_1 | val_2_1 | val_3_1 | val_1_2 | val_2_2 | val_3_2
---|------|---------|---------|---------|---------|---------|--------
1 | 2001 | 10 | 11 | 12 | 20 | 21 | 22
2 | 2002 | 30 | 31 | 32 | 40 | 41 | 42