I have a function fnEp_
that takes a data.table
column, a data.table
and a logical
type. I am trying to make the function iterate over each element of eltIndexEnriched$Max
(see below). It works but it is very slow. I wonder if there is a better way to iterate or perhaps a setting to make it faster.
Here I am creating a column EP_1
in the data.table
from a function called fnEP_
using lapply
:
eltIndexEnriched <- eltIndexEnriched[, EP_1 :=
lapply(Max, fnEp_, dt = eltIndexEnriched, Indemn_Bool = TRUE)]
fnEp_ <- function(Att_, dt, Indemn_Bool) {
if (Indemn_Bool == TRUE) {
retval <- (1 - exp(-1 * sum(dt$Rate * (ifelse(Att_ > dt$Max,
0, 1 - pbeta(Att_ / dt$Max, dt$Alpha, dt$Beta))))))
} else {
retval <- dt[Mean > Att_, 1 - exp(-1 * sum(Rate))]
}
return(retval)
}