How to generate multiple matrix in R

前端 未结 3 592
一生所求
一生所求 2021-01-27 06:22

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 
相关标签:
3条回答
  • 2021-01-27 06:32
    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

    0 讨论(0)
  • 2021-01-27 06:44
     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
    }
    
    0 讨论(0)
  • 2021-01-27 06:49

    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
    
    0 讨论(0)
提交回复
热议问题