several loops in only one loop with changing parameters over loops in R -
several loops in only one loop with changing parameters over loops in R -
assume illustration purposes have next row vector newdata.forgm[1,]
:
x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 3.804125 4.480942 5.023256 5.453471 5.790166 6.048769 6.24211 6.380872 6.473965 6.528837 x12 .... x36 6.55172....4.26642
where x1 removed.
i want create next matrix next row vectors 30 products each row:
(x2*x3)^(1/2) (x2*x3*x4)^(1/3) (x2*x3*x4*x5)^(1/4) ... (x2*...*x32)^(1/31) (x3*x4)^(1/2) (x3*x4*x5)^(1/3) (x3*x4*x5*x6)^(1/4) ... (x3*...*x33)^(1/31) (x4*x5)^(1/2) (x4*x5*x6)^(1/3) (x4*x5*x6*x7)^(1/4) ... (x4*...*x34)^(1/31) (x5*x6)^(1/2) (x5*x6*x7)^(1/3) (x5*x6*x7*x8)^(1/4) ... (x5*...*x35)^(1/31) (x6*x7)^(1/2) (x6*x7*x8)^(1/3) (x6*x7*x8*x9)^(1/4) ... (x6*...*x36)^(1/31)
i have next r code:
n=31 prod.spe <- function(x) { answ <- null for(i in 2:n) { answ <- c(answ, (prod(x[1:(i+0)]))^(1/i)) } return(answ) } prod.spe(newdata.forgm[1,]) prod.spe <- function(x) { answ <- null for(i in 2:n) { answ <- c(answ, (prod(x[2:(i+1)]))^(1/i)) } return(answ) } prod.spe(newdata.forgm[1,]) prod.spe <- function(x) { answ <- null for(i in 2:n) { answ <- c(answ, (prod(x[3:(i+2)]))^(1/i)) } return(answ) } prod.spe(newdata.forgm[1,]) prod.spe <- function(x) { answ <- null for(i in 2:n) { answ <- c(answ, (prod(x[4:(i+3)]))^(1/i)) } return(answ) } prod.spe(newdata.forgm[1,]) prod.spe <- function(x) { answ <- null for(i in 2:n) { answ <- c(answ, (prod(x[5:(i+4)]))^(1/i)) } return(answ) } prod.spe(newdata.forgm[1,])
how can write 1 for-loop in r accounts alter in 0,1,2,3,4 , 1,2,3,4,5 (see above how these numbers change). feedback appreciated.
here reply question:
n=31 prod.spe1 <- function(x) { asnansw <- null for(i in 2:n) { (j in 0:4) { answ <- c(answ, (prod(x[(j+1):(i+j)]))^(1/i)) } } return(answ) } matrix((prod.spe1(newdata.forgm[1,])),nrow=5,ncol=30)
r
Comments
Post a Comment