0

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) 
}
Arun
  • 116,683
  • 26
  • 284
  • 387
  • Your example is not reproducible, but it looks like `Indemn_Bool` might be vectorizable. – Vlo Jun 26 '14 at 20:54
  • I made a start at editing but yes see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Matt Dowle Jun 26 '14 at 20:56
  • 3
    It seems you haven't read the documentation or browsed many of the questions in this tag. – Matt Dowle Jun 26 '14 at 20:58

0 Answers0