简体   繁体   中英

How to generate multiple matrix in R

I have gotten two lists of values in R.

daily_max_car: (List 1)
21 21 22 22 22 22 21 
daily_0.8: (List 2)
16 17 17 17 18 17 17 

Trying to write a For Loop in R-Studio to generate multiple matrix by using the one of the values from these two lists (One by One).

Here is the code I have been using to generate one matrix!

 Lambda <- 21 (From List 1)
 Mue <- 4
 Rho <- Lambda/Mue
 N <- 16 (From List 2)

All of these four parameters will be used in the "calculatewq" Function.

calculatewq <- function(c)
{....Some thing happening }

##Create Matrix
matrix1 <- matrix(0,Lambda,4)
matrix1[,1] <- 1:Lambda
### Create a column of matrix with repeated "N"
rep.row<-function(x,y)
{matrix(rep(x,each=y),nrow=y)}
created_mar_1 <- rep.row(N,Lambda)
car_n<- created_mar_1-matrix1[,1]
created_mar_3 <- rep.row(69*60*24,Lambda)

## Add into Matrix
for (i in 1:Lambda)
{matrix1[i,2] <- calculatewq(i)[2]
matrix1[i,3] <- calculatewq(i)[5]
matrix1[,4] = car_n*created_mar_3}`

Once I change one of the parameters it will generate a new matrix. Thus, how can I write a for loop to generate multiple matrix while I am putting different value in Lambda and N.

Thank you so much! Sampson

I removed for loop inside calculatewq function. Please make sure you needed a for loop in it.

myfun <- function(Lambda, N, mu )
{
  # browser()
  var1 <- seq_len( Lambda )
  var2 <- ( rep( N, each = Lambda) ) - var1
  var3 <- rep( 69*60*24, each = Lambda )
  var4 <- var2 * var3

  fun_vals <- do.call( 'rbind',
                       lapply( var1, function( x ) calculatewq( x, Lambda = Lambda, N = N, mu = mu ) ) )

  mat <- matrix( NA, nrow = Lambda, ncol = mu )
  mat[, 1] <- var1
  mat[, 2] <- fun_vals[, 'Wq']
  mat[, 3] <- fun_vals[, 'customer_serviced']
  mat[, 4] <- var4
  return(mat)
}

calculatewq <- function( x, Lambda, N, mu )
{
  # browser()
  Rho <- Lambda / mu
  p0_inv <- ( Rho^x * (1-(( Rho/x )^( N-x+1)))) / (factorial( x ) * ( 1 - ( Rho / x ) ) )
  p0_inv <- p0_inv + ( Rho^x) / factorial( x )
  P0 <- 1/p0_inv
  Lq <- ( Rho^(x+1)) * (1-((Rho/x)^(N-x+1))-((N-x+1)*(1-(Rho/x))*((Rho/x)^(N-x))))*P0/(factorial(x-1)*(x-Rho)^2)
  Wq <- 60*Lq/Lambda
  Ls <- Lq + Rho
  Ws <- 60*Ls/Lambda
  PN <- (Rho^N)*P0/(factorial(x)*x^(N-x))
  customer_serviced <- (1 - PN)*100
  a <- cbind( Lq, Wq, Ls, Ws, customer_serviced )
  return(a)
}

mu <- 4
res <- Map( myfun, 
            list( 21 ,21, 22, 22 ,22, 22 ,21 ),
            list( 16, 17, 17, 17, 18, 17 ,17 ),
            mu)
head( res[[1]])
#      [,1]      [,2]     [,3]    [,4]
# [1,]    1 42.184874 19.04762 1490400
# [2,]    2 38.241748 38.09526 1391040
# [3,]    3 33.339271 57.13862 1291680
# [4,]    4 26.014138 75.70348 1192320
# [5,]    5 16.339462 89.88989 1092960
# [6,]    6  9.121053 96.32498  993600
daily_max_car <- list(21,21,22,22,22,22,21)
daily_0.8 <- list(16,17,17,17,18,17,17)

myfunction <- function(Lambda, N){
  Mue <- 4
  Rho <- Lambda/Mue
  df <- as.data.frame(matrix(0, ncol = 4, nrow = Lambda))
  names(df) <- c("A","B","C","D")
  df[,1] <- 1:Lambda
  df[,2] <- N
  df[,3] <- df[,1] - df[,2] 
  df[,4] <- 69*60*24
  return(df) 
}

myfunction(21,16)
result <- mapply(myfunction, daily_max_car, daily_0.8)

Result

在此处输入图片说明

 Lambda <- 21
 Mue <- 4
 Rho <- Lambda/Mue
 N <- 19

matrix1 <- matrix(0,Lambda,4)
matrix1[,1] <- 1:Lambda
rep.row<-function(x,y)
{
matrix(rep(x,each=y),nrow=y)
}
created_mar_1 <- rep.row(N,Lambda)
car_n<- created_mar_1-matrix1[,1]
created_mar_3 <- rep.row(69*60*24,Lambda)
calculatewq(7)

calculatewq <- function(c)
{
P0inv <- (Rho^c*(1-((Rho/c)^(N-c+1))))/(factorial(c)*(1-(Rho/c)))
for (i in 1:c-1) 
{
P0inv = P0inv + (Rho^i)/factorial(i)
}
P0 = 1/P0inv
Lq = (Rho^(c+1))*(1-((Rho/c)^(N-c+1))-((N-c+1)*(1-(Rho/c))*((Rho/c)^(N-   c))))*P0/(factorial(c-1)*(c-Rho)^2)
Wq = 60*Lq/Lambda
Ls <- Lq + Rho
Ws <- 60*Ls/Lambda
PN <- (Rho^N)*P0/(factorial(c)*c^(N-c))
customer_serviced <- (1 - PN)*100
a <- cbind(Lq,Wq,Ls,Ws,customer_serviced)
return(a)
}
for (i in 1:Lambda)
{
matrix1[i,2] <- calculatewq(i)[2]
matrix1[i,3] <- calculatewq(i)[5]
matrix1[,4] = car_n*created_mar_3
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM