我定义两个函数如下:
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))
}
这两个函数都返回一个列表,因为我的意图是使用这些列表来携带相关值。一个例子是使用返回的列表load()
作为函数的参数erlang.C()
。我认为这很有用,因为erlang.C()
同时使用load$load
值load$servicetime
和值,并且通过使用列表,没有一起使用错误参数值的风险。
现在考虑以下带有输入数据的小标题:
library(tidyverse)
example <- tibble(rate = c(0.4, 0.7,1.8),
servicetime = c(0.3, 0.75, 1.2))
在这个小标题中包含负载很容易:
example <- example %>%
mutate(load = load(rate, servicetime)$load)
但是,继续使用 dame 逻辑是行不通的:
example <- example %>%
mutate(load = load(rate, servicetime)$load,
waiting = erlang.C(load = load(rate, servicetime), 2)$waitingtime)
- 如何将结果添加
erlang.C()
到 tibble? - 是否可以(如果是,如何)使用
mutate()
将两个列表条目添加到小标题而不调用erlang.C()
两次?