[R] Can somebody suggest how to achieve following data manipulation?
Christofer Bogaso
bogaso.christofer at gmail.com
Mon Dec 10 21:50:15 CET 2012
Dear all,
Let say I have following data:
RawData <- matrix(1:101, nr = 1); colnames(RawData) <- c("ASD",
as.character(as.yearmon(seq(as.Date("2012-03-01"), length.out = 100, by
= "1 month")))); rownames(RawData) <- "XYZ"
CutOffDate <- as.Date("2012-09-01")
NewDateSeries <- as.character(as.yearmon(seq(CutOffDate, to =
as.Date("2025-01-01"), by = "1 month")))
ResultMat <- matrix(NA, 1, length(NewDateSeries))
colnames(ResultMat) <- NewDateSeries
rownames(ResultMat) <- "Result"
RawData
ResultMat
Now I need to pass the elements of 'RawData' to 'ResultMat' in following
way:
Consider the column 'Sep 2012' of 'ResultMat' (i.e. the first column).
The element for this column will be sum of all elements of columns less
than or equal to 'Sep 2012' of 'RawData' and including the column 'ASD'.
Therefore, the column 'Sep 2012' of 'ResultMat' will have the element as
"1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36".
And remaining columns of 'ResultMat'will get the data from the same
column names of 'RawData'. Therefore the 2nd column of 'ResultMat' will
have the element as 9 and so on.
I need to adopt above filling policy for arbitrary 'CutOffDate'.
Ofcourse this can be implemented using a 'for' loop. However I believe
there is some better R-way to do that.
Can somebody help me to achieve that?
Thanks and regards,
More information about the R-help
mailing list