I want to find the cumulative sum of a bunch of columns as explained in the R: ddply repeats yearly cumulative data. That is,
ddply(mydf, "year", transform,
cumsum1 = cumsum(myvalue1),
cumsum2 = cumsum(myvalue2))
I tried the following.
Solution 1:
1.Created a list of destination names for cumulative sum and a list of source names.
2.Ran ddply(mydf,"year",transform,dstnList=srcList)
3.Getting the following error:
"arguments imply differing number of rows: 1385, 280
In addition: Warning message:
In eval(expr, envir, enclos) : NAs introduced by coercion"
Solution 2 :
1.Create a following function.
findCumSum<-function(srcdf,columnlist){
for (i in 1:length(columnlist)){
ddply(srcdf,"g_id",transform,cumsum(names(srcdf)[columnlist[i]]))
}
srcdf
}
2.Call the function with list of srcList. findCumSum(mydf,srcIdxList)
;
I am getting the following error
"Error in eval(expr, envir, enclos) : object 'srcdf' not found"
Let me know how to solve the problem.