Passing list of named parameters to function?

前端 未结 2 1615
野趣味
野趣味 2020-12-05 06:18

I want to write a little function to generate samples from appropriate distributions, something like:

makeSample <- function(n,dist,params)
values <- m         


        
2条回答
  •  误落风尘
    2020-12-05 07:02

    c(...) has a concatenating effect, or in FP parlance, a flattening effect, so you can shorten the call; your code would be:

    params <- list(min=0, max=1)
    do.call(runif, c(n=100, params))
    

    Try the following comparison:

    params = list(min=0, max=1)
    str(c(n=100, min=0, max=1))
    str(list(n=100, min=0, max=1))
    str(c(list(n=100),params))
    str(c(n=100,params))
    

    Looks like if a list is in there at any point, the result is a list ( which is a desirable feature in this use case)

提交回复
热议问题