I have a data frame with 3 columns (excerpt below):
df <- data.frame( id = c(1,1,1,2,2,2), Year = c(2007, 2008, 2009, 2007, 2008, 2009), A = c(5, 2, 3, 7, 5, 6), B = c(10, 0, 50, 13, 17, 17) ) df
I would like to have this:
df_needed <- data.frame( id= c(1, 2), A_2007 = c(5, 7), B_2007 = c(10, 13), A_2008 = c(2, 5), B_2008 = c(0, 17), A_2009 = c(3, 6), B_2009 = c(50, 17) ) df_needed
I am familiar with reshape and tidyR , but I do not think they can control this conversion.
Is there a proper way to do this, or do I need to do this using a special function?
Edit: this example has been edited to improve the example with more than 1 record in the final data set.