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)
- How can I add the result of
erlang.C()
to the tibble? - Is it possible (and if yes, how) to use
mutate()
to add both list entrys to the tibble without callingerlang.C()
twice?