I have this data
structure(list(Year = structure(1:11, .Label = c("2008", "2009",
"2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017",
"2018"), class = "factor"), Age0 = c(1.85714285714286, 0.4, 0.485714285714286,
1.1, 2.42857142857143, 0.257142857142857, 0.0428571428571429,
0.314285714285714, 0.716666666666667, 0.833333333333333, 2.51666666666667
), Age1 = c(NA, NA, 0.0909090909090909, NA, 0.115384615384615,
0.157894736842105, 0.0666666666666667, 0.239130434782609, 0.0862068965517241,
0.135802469135802, 0.154639175257732), Age2 = c(NA, NA, 0.181818181818182,
0.25, 0.0384615384615385, 0.315789473684211, 0.0666666666666667,
0.260869565217391, 0.258620689655172, 0.209876543209877, 0.298969072164948
), Age3 = c(NA, NA, NA, NA, 0.0769230769230769, 0.157894736842105,
0.533333333333333, 0.282608695652174, 0.224137931034483, 0.160493827160494,
0.298969072164948), Age4 = c(NA, NA, NA, NA, 0.153846153846154,
0.157894736842105, 0.133333333333333, 0.173913043478261, 0.137931034482759,
0.123456790123457, 0.134020618556701), Age5 = c(NA, NA, NA, NA,
NA, 0.105263157894737, 0.2, 0.152173913043478, 0.103448275862069,
0.0987654320987654, 0.0618556701030928), Age6 = c(NA, NA, NA,
NA, NA, NA, NA, 0.108695652173913, 0.120689655172414, 0.037037037037037,
0.0824742268041237), Age7 = c(NA, NA, NA, NA, NA, NA, NA, 0.0652173913043478,
0.0689655172413793, 0.0617283950617284, 0.0103092783505155),
Age8 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.0493827160493827,
0.0103092783505155), Age9 = c(NA, NA, NA, NA, NA, NA, NA,
NA, NA, 0.0123456790123457, 0.0309278350515464)), class = "data.frame", row.names = c(NA,
-11L))
I'm trying to do something very similar to this... Individual shift of each column in a matrix
using this code...
wi.v.hog.prop2 <- setDT(wi.v.hog.prop2)
for (i in 3:length(wi.v.hog.prop2)) wi.v.hog.prop2[,i] <- shift(wi.v.hog.prop2[,i,with=F],(i-1),NA,"lead")
I got it to start shifting the correct column (Age 1), but it shifts it up too far. It should only shift up one place, then Age2 should shift up 2 places.
It looks like this code is starting at the right age but basing the amount of shift on the number of columns the target column is away from the first column. So since Age1 is the 3rd column it is shifting it up 2 positions, Age2 is the 4th column its shifting it up 3 positions. I just need one less shift. Thanks.