如何将列表添加到tibble

时间:2017-11-16 19:13:25

标签: r list vectorization tidyverse tibble

我定义了两个函数如下:

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)
  1. 如何将erlang.C()的结果添加到tibble?
  2. 是否有可能(如果是,如何)使用mutate()将两个列表委托添加到tibble而不调用erlang.C()两次?

0 个答案:

没有答案