i want to calculate cumulative loss at each set of iterations. i.e. let's say my n_sim <-1000
and x=rep(3,n_sim)
.
in the above case, set of iteration is 3 and number of set of iteration is 1000, so total 3000 (1000*3) iterations.
Let's define below:
Iter <- rep(1:n_sim, x)
, Val <-rnorm(3*n_sim)
, df<-data.frame(Iter,Val)
.
problem:i want to calculate sum of Val
taking each set of iteration meaning as here a set consist of 3 iterations, i want to take Val
of first 3 iterations and calculate sum and so on. So i will have a final vector of length 1000 containing sum of Val
of each set.
I have tried below using for
loops, but it is very very slow while doing 1mn simulations.
sum_value <-array(0,c(n_sim))
for(i in 1: n_sim) {
sum_value[i] = df[df$Iter==i,] %>% .$Val %>% sum
}
Anyone has better idea how to fast this process.