vendredi 11 janvier 2019

Random numbers in a column based on another column's value

Good day all,

I'm struggling with creating a column which would populate the values using a random value generating value function which takes another column's value as an argument.

A bit of a context - I have a data table with lead time in a column:

dt <- data.table(Item = rep(123456,each = 1000), LT = rnorm(1000,mean = 10, sd = 3)

and a function:

rand_ddlt_norm <- function(Lt,est11,est12){
  sign(Lt) * ( sum( rnorm( floor(abs(Lt)), est11, est12) ) +
                 rnorm(1, est11, est12) * ( abs(Lt)%%1) )
}

The above function is designed to calculate demand during the lead time for each row.

Unfortunately, I cannot do that:

dt[,ddlt := rand_ddlt_norm(LT, mean = 100, sd = 30)]

because all rows will be populated with the same number.

I could obviously put it into a loop, but for 10,000 iterations, 20,000+ products and numerous distribution types, the computation time is getting ridiculous.

I would graciously welcome any suggestions about how this code could be optimised without running a loop.




Aucun commentaire:

Enregistrer un commentaire