How to use multiple arguments in a function?

Hi,
I have a function,func1.

func1=function(dat,T_period){
  dat1=sort(dat,decreasing = T)
  K_T=(-sqrt(6)/pi)*(0.5772+log(T_period, base = exp(1)))
  X_T=mean(dat)+K_T*sd(dat)
  return(X_T)
}
dat1=list(sample(100,20),    
sample(100,10))
T_period1=as.list(c(2,5,10))

I want to get different outputs in the following way:
Each T_period1 for a given dat1, I am expecting an output X_T.
Therefore, how can I get 6 X_T (3 T_periods for each dat1).

Here is one method using functions from base R.

dat1=list(sample(100,20), sample(100,10))
T_period1=as.list(c(2,5,10))

func1=function(dat,T_period){
  LongDat <- rep(dat, each = length(T_period))
  LongT <- rep(T_period, length(dat))
  InnerFunc <- function(dat, T_period) {
    dat1=sort(dat,decreasing = T)
    K_T=(-sqrt(6)/pi)*(0.5772+log(T_period, base = exp(1)))
    X_T=mean(dat)+K_T*sd(dat)
    return(X_T)
  }
  mapply(InnerFunc, LongDat, LongT)
}
func1(dat1, T_period1)
#> [1]  20.583098  -1.139969 -17.572836  24.337242   4.384327 -10.709470

Created on 2022-05-04 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.