r - Use Final Value In Iterative Process -


here code working with:

## round 1 b <- c(5000,2500) r <- c(2,2.1)  pm <- c(200,240)  <- b*(r/1200)  pi <- pm-i end <- b-pi   end  ## round 2  b1 <- end  r1 <- (2.3,2.3)  i1 <- b1*(r/1200) pi1 <- pm-i1  end1 <- b1-pi1  end1 

this produces proper output, shown below in post. but, can see how might annoying 30 rounds thought able use *sapply function. attempted do:

r2<- matrix(c(2,2.1,2.3,2.3),nrow=2) i2 <- sapply(b, function (x)   x*(r2/1200) )  pi2 <- sapply(i, function (l)   pm-l )   end2 <- b-pi2 end2 

which gives:

[1,] 4808.333 4804.375 [2,] 2268.333 2264.375 

this did not produce desired output is:

[1,] 4808.333 4616.347 [2,] 2264.375 2028.338 

how go doing because "end" variables need updated "b" value shown in starred part of code below.

    **b1 <- end**      r1 <- 2.3      i1 <- b1*(r/1200)     pi1 <- pm-i1      end1 <- b1-pi1      end1 

thank time.

edit

pierre gives answer below, have question how process "n" number of times. pierre's answer works specific question. but, trying make work when "r" , "r1" joint in matrix like

rate <- rbind(r,r1)  

and use sapply function kind of like:

t(sapply(b, function(x) {b <- b - (pm - b*(rate/1200));b})) 

any suggestions?

this situation loops shine:

rlist <- list(r, r1) x <- vector("list", 2) for(i in 1:2) {b <- b - (pm - b*(rlist[[i]]/1200)); x[[i]] <- b} `names<-`(as.data.frame(x), c('x', 'y')) #         x        y #1 4808.333 4617.549 #2 2264.375 2028.715 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -