I have the following table:
Name, id, year, sumofloss
a, 11, 2001, 1
a, 11, 2002, 2
a, 11, 2003, 3
a, 11, 2004, 4
b, 12, 2001, 5
b, 12, 2002, 6
b, 12, 2003, 7
b, 12, 2004, 8
So suppose I have two account, called a, and b, and each has a loss number for each year 2001, 2002, 2003, 2004. And now I want to manipulate the table and get a following table like this:
Name, id, 2001, 2002, 2003, 2004
a, 11, 1, 2, 3, 4
b, 12, 5, 6, 7, 8
So the new table will have each of the year value as new column, and the value of each column is the previous sumofloss column, it's like extracting the year values as new column.
I tried group_by, but found it hard to extract the value of year as column just by one function, but instead should I manually set new column using the value of year line by line? If I have 10 or more years, that would be very hard-coded. Is there a way to do this transformation of table easier?