Home > OS >  Random Distribution with Params within Data Frame
Random Distribution with Params within Data Frame

Time:02-08

I'm attempting to generate random losses within a data frame, using pre-specified variable. What I'm interested in is the "rand.num" variable - what is a better / more efficient way to generate that random number? I get what I'm looking for using the below, but when I run it with my full table, and for many simulations, I'm having run time issues.

data <- as.data.frame(matrix(c(1, 2500, 2500, 5000), 2, 2)) #take this as given

colnames(data) <- c("Lower", "Upper") #lower & upper bound of uniform distribution

rand.num.1 <- rdunif(1, data[1,1],  data[1,2])

rand.num.2 <- rdunif(1, data[2,1],  data[2,2])

data$rand.num <- c(rand.num.1, rand.num.2)

print(data)

Any help would be much appreciated!

CodePudding user response:

Do the random sampling all in one go with runif and truncate:

data$rand.num <- floor(runif(nrow(data), data$Lower, data$Upper   1))
  •  Tags:  
  • Related