I have a data frame and I would like to add every three rows of it. Then, to apply cumprod to the rows in order to have a new data.frame with the resulting new rows.
At the end I will have only one third of the number of rows.
Below you can find some code that I wrote. I have tried to look for the class of the objects and to replicate a code that worked for a vector, not a matrix.
XYZ<-read.xlsx2("XYZ.xlsx",1)
XYZ.CUT<-aggregate(XYZ~gl(nrow(XYZ)/3, 3), data.frame(XYZ), sum)
F.XYZ<-apply(t(XYZ.CUT+1),1,cumprod)
This is what I have:
X Y Z
-0,01% 0,32% 0,11%
-0,04% 0,01% 0,45%
-0,11% -0,06% 0,03%
0,03% -0,04% 0,45%
0,02% 0,04% 0,30%
-0,07% -0,11% 0,11%
-0,12% -0,13% 0,30%
-0,01% -0,07% 0,04%
-0,37% 0,08% 0,05%
first I want:
X Y Z
-0,16% 0,25% 0,59%
-0,02% -0,11% 0,86%
-0,50% -0,12% 0,39%
and after adding 1 to every element:
X Y Z
(1-0,16%) (1+0,25%) (1+0,59%)
(1-0,02%) (1-0,11%) (1+0,86%)
(1-0,50%) (1-0,12%) (1+0,39%)
I would like to make a cumprod with the rows:
X Y Z
x1 y1 z1
x1*x2 y1*y2 z1*z2
x1*x2*x3 y1*y2*y3 z1*z2*z3
Advanced greetings.