run a function with multiple values for more than one arguments that are not the first ones

前端 未结 2 1087
予麋鹿
予麋鹿 2021-01-07 02:40

In R, if I have a function

myfun<-function(ys, T, N, beta, gamma, sigma) {...}

where beta and gamma are scalars.

If I have fixe

相关标签:
2条回答
  • 2021-01-07 03:28

    Look at the mapply and Vectorize functions along with expand.grid for the all combinations part.

    0 讨论(0)
  • 2021-01-07 03:38

    Let's use a handy multi-argument function like dt() as an example. We will ("mult")-supply 2 arguments in "parallel" and ("const")-supply fixed values for the other two. Build a dataframe with expand.grid for the multi-argument lists and then pass it to mapply with appropriate names and use MoreArgs for the remaining arguments;

    gamma <- 1:3 
    beta <- 1:4
    gb.df <-expand.grid(gamma=gamma, beta=beta)
    mfun <- dt
    mapply ("mfun", x=gb.df$gamma, df=gb.df$beta, MoreArgs=list(ncp=1, log=FALSE) )
        [1] 0.26355595 0.14379745 0.07896827 0.31785177 0.17910975 0.08636815
        [7] 0.34118167 0.19555939 0.08572842 0.35411486 0.20513016 0.08355531
    
    0 讨论(0)
提交回复
热议问题