I recently asked a question that started me in the right direction, but left me with a few unsolved issues.
I have a data.frame with final growth measurement values listed in the last row (RBH). Each sample is in a separate column and has a different end year of observation (2010, 2011, or 2012). Basically, I need to apply the final total growth measurement (RBH value) to the last year with a measurement, and then subtract each previous year's annual growth measurement from the current growth measurement to recreate how large the sample was each year.
The solution given in the previous question allows me to create a cumulative sum to subtract from each measurement, however it doesn't treat NA or NaN values appropriately. If a given sample has the following measurements, then the output should be as follows:
Sample measurements:
2009 - 1.2
2010 - 1.8
2011 - NaN
2012 - NaN
RBH - 60.5
Intended Output:
2008 - 57.5
2009 - 58.7
2010 - 60.5
2011 - NaN
2012 - NaN
The code I've been using that was suggested by another user is as follows:
cumsum.alt <- function(x){
res <- NaN*seq(x)
for(i in seq(x)){
if(sum(is.na(x[1])) == i){
res[i] <- NaN
} else {
res[i] <- sum(x[1:i], na.rm=TRUE)
}
}
res
}
df1[is.na(df1)] = 0 ##This is how you're actually treating it!
df1 = data.frame(df1)
df2 = apply(df1[nrow(df1):1,], 2, function(x) c(x[1], x[1]-cumsum.alt(x[-1])))
df2 = df2[nrow(df2):1,]
This works for the cumulative sum, but doesn't deal with NA values. Any suggestions would be greatly appreciated.