0

I am defining two functions as follows:

load <- function(rate, servicetime){
    # Calculates Offered Load in Erlang
    # rate: Rate of arrivals
    # servicetime: Mean time for service
    a = rate * servicetime
    return(list(load = a, rate = rate, servicetime = servicetime))
}

erlang.C <- function(load, servers){
    # Returns: 
    # -- p: Percentage of customers who have to wait
    # -- waitingtime: Mean waiting time of a customer
    # load: Offered Load in Erlang from load()
    # servers: Number of serves in use
    if(load$load >= servers){
        warning("Erlang.C not solvable. Increase number of servers.")
        return()
    }
    top <- (load$load^servers) / (factorial(servers) * (1 - (load$load / servers)))
    bottom.summands <- function(load, server){
        # Helper function
        x <- (load$load^server) / factorial(server)
        return(x)
    }
    s <- c(0, seq(servers-1))
    bottom <- bottom.summands(load, s)
    bottom <- sum(bottom)
    bottom <- top + bottom
    eC <- top / bottom
    wt <- (load$servicetime * eC) / (servers - load$load)
    return(list(p = eC, waitingtime = wt))
}

Both functions return a list since my intention is to use these lists to carry related values. An example for this is the use of the list returned by load() as parameter of the erlang.C() function. I consider this useful since erlang.C() uses both the load$load value as well as the load$servicetime value and by using lists there is no risk of using wrong parameter values together.

Now consider the following tibble with input data:

library(tidyverse)
example <- tibble(rate = c(0.4, 0.7,1.8),
                  servicetime = c(0.3, 0.75, 1.2))

It is easy enough to include the load in this tibble:

example <- example %>%
               mutate(load = load(rate, servicetime)$load)

However, continuing with the dame logic does not work:

example <- example %>%
               mutate(load = load(rate, servicetime)$load,
                      waiting = erlang.C(load = load(rate, servicetime), 2)$waitingtime)
  1. How can I add the result of erlang.C() to the tibble?
  2. Is it possible (and if yes, how) to use mutate() to add both list entrys to the tibble without calling erlang.C() twice?
F. Privé
  • 11,423
  • 2
  • 27
  • 78
fr3d-5
  • 792
  • 1
  • 6
  • 27
  • Your code is not working, `load$load` is meant to be a vector and this cause problems with `load$load >= servers` and in `(load$load^servers)`. – F. Privé Nov 16 '17 at 20:39
  • 2
    Could you try simplifying the question as much as you can please? Ideally the business logic of your code shouldn't appear at all, it should be about basic data structures. – Lionel Henry Nov 16 '17 at 22:04

0 Answers0