I have a data table that looks like this:
ID Start_Year Payment_2010 Payment_2011 Payment_2012 Payment_2013
1 2010 19 15 NA 11
2 2012 22 24 26 23
3 2012 NA NA NA 05
4 2011 17 NA 14 NA
It carries on like this for many rows, with about 20 years of Payment columns in total.
I need to re-shape it, using R or SQL, with the current Payment columns replaced by ones in which the first column (Payment_1) represents the value in the Start_Year column, and the columns go on sequentially from there. For example, I would like to get the table above to look like this:
ID Start_Year Payment_1 Payment_2 Payment_3 Payment_4
1 2010 19 15 NA 11
2 2012 26 23 NA NA
3 2012 NA 05 NA NA
4 2011 NA 14 NA NA
So, any Payment info in the first table that falls before the start year can be discarded. Where payment info is unavailable, the value should be NULL or NA (not zero, as these are different to NA in my data).
I started by trying to use dynamic SQL to create new columns based on the Start_year column, but got stuck. I've got closer by using spread and gather commands from the dplyr package in R, but my pipe keeps going wrong and I can't find a way to discard the Payment info that comes before the start_year value.
Any help much appreciated, many thanks in advance.
(I understand this is a strange format to want to have the data in, but that is what I need to do).