Home > Enterprise >  How to save results at the end of each for-loop
How to save results at the end of each for-loop

Time:06-13

I have this df:

structure(list(a = c(0.916290731874155, 2.89037175789616, 2.14658084451746, 
-2.62103882411258, -2.07944154167984, 2.00533356952611, -1.24319351747922, 
0.42744401482694, 1.29532258291416, -2.03292152604494, -0.606135803570316, 
-0.693147180559945), b = c(0.550046336919272, 0.228258651980981, 
-0.577634293438101, 0.135801541159061, 0.644357016390513, -2.30258509299405, 
-0.0870113769896297, 1.71297859137494, 0.17958557697508, -1.65140211153313, 
1.31218638896617, 2.19935904986485), c = c(0.0988458346366325, 
-3.34403896782221, 1.99243016469021, 0.737598943130779, 0.178691788743376, 
2.20727491318972, -1.40242374304977, -1.256836293883, -2.16905370036952, 
2.91777073208428, 0.138586163286146, -0.946143695023836), d = c(2.57963390914446, 
-5.14458326660599, 1.83258146374831, 1.15057202759882, 0.0613689463762919, 
-2.23359222150709, 4.34236137828145, -3.44854350225935, 1.29098418131557, 
-0.356674943938732, -0.21868920096483, -0.810930216216329), e = c(1.65140211153313, 
0.220400065368459, -0.044951387862266, 0.0773866636154201, -1.49877234454658, 
1.36219680954083, 2.07179039494432, -3.07731226054641, -0.916290731874155, 
1.65822807660353, 0.451985123743057, -0.810930216216329)), class = "data.frame", row.names = 2:13)

and this script that is all in a for-loop:

rs <- 4
sr <- 2
for (t in ((rs 1):(12-sr))) {
z <- 0
R <- Map(` `, list((t-rs):(t-1)), (0:z))
cmin <- t(as.matrix(rep(NA, ncol(dx))))
cdf_mat <- matrix(NA, length(R), ncol(dx))
sq <- list()
for (r in seq(R)) {
  for (f in seq(ncol(dx))) {
    s_df<- rbind(0,dx[R[[r]],])
    df_cum <- sapply(s_df, function(x) ((cumsum(x))   1))
    x <- df_cum[,f]
    y <- df_cum[,-f]
    dif_2 <- (x - y)^2
    cmin[f] <- which.min(colSums(dif_2))
    dif_3 <- as.matrix(dif_2[,cmin[f]])
    cdf_mat[r,f] <- 
      if (f <= cmin[f]) {
        cmin[f]   1
      } else { 
        cmin[f]
      }
    sq <- c(sq, list(sqrt(dif_3)))
    sqmat <- do.call(cbind, sq)
    sd <- (colSums(sqmat))/t
  }
}
.
.
.
.
for (m in seq(M)) {
  sb_bs <- rbind(sell_o[m,], buy_c[m,],buy_o[m,],sell_c[m,])
}
rsb <- matrix(NA, length(M), ncol(dx))
rsb [] <- apply(sb_bs , 2 ,
 function(x) if((x[[1]] == 1) | (x[[2]] == 1)) 0 else x[[1]] - x[[2]])
rbs <- matrix(NA, length(M), ncol(dx))
rbs [] <- apply(sb_bs , 2 ,
 function(x) if((x[[3]] == 1) | (x[[4]] == 1)) 0 else x[[4]] - x[[3]])
}

I'm not so confident with R-Studio, so I haven't been able to simplify the script for the question and to make it clear what I need. I need to save in a matrix the results of "rsb" and "rbs" for each loop. So the result I expect is:

rsb
      [,1]   [,2]        [,3]        [,4]         [,5]
[1,]   0     0           2.32        0            0
[2,]   0     0           1.402424    4.342361     3.664782
[3,]   0     1.712979    0           5.744785     3.077312
[4,]   0     4.790291    2.736767    0            0
[5,]   0     0           0           0.5490468    0
[6,]   0     0           0           0            0

rbs
      [,1]   [,2]        [,3]         [,4]        [,5]
[1,]   0     0           2.35         0           0
[2,]   0     0           4.342361     1.402424    3.664782
[3,]   0     3.077312    0            5.744785    0.427444
[4,]   0     4.790291   -0.3485777    0           0
[5,]   0     0           0            1.8506      0
[6,]   0     0           0            0           0

How can I do to get these two matrices? Thank you everyone for the help!

CodePudding user response:

Try with the following :

First, add the rbs_final and rsb_final as empty vectors at the beginning of your code, and also add rsb_final <- append(x = rsb_final, rsb) and rbs_final <- append(x = rbs_final, rbs) near the end of the loop so it looks like this:

rbs_final <- c()
rsb_final <- c()
rs <- 4
sr <- 2
for (t in ((rs 1):(12-sr))) {

# WHAT YOU ALREADY HAVE INSIDE YOUR FOR LOOP

rsb_final <- append(x = rsb_final, rsb)
        
rbs_final <- append(x = rbs_final, rbs)

}

Then, after your for loop, add this :

rsb <- matrix(rsb_final, nrow = 6, ncol = 5, byrow = T) ; rsb

rbs <- matrix(rbs_final, nrow = 6, ncol = 5, byrow = T) ; rbs

Which will create and print in console your desired output

EDIT: typo

  • Related