Add max value to simulation in R

Viewed 63
# Here's how I simulate my dataset:
n.days <- 1000
n.indvs <- 10000
rate.decay <- 0.01

set.seed(1)
system.time(
  sim_data <- replicate(n.days, rexp(n.indvs, rate.decay))
)

I want to add a max for all values in the dataset (max = 700). How can I do that?

2 Answers

You can use pmin to set the upper limit to the dataset, i.e.,

sim_data <- pmin(sim_data,700)

Assuming you just want to cap it at 700 (i.e. round everything down to 7000 if it's above)

sim_data <- ifelse(sim_data > 700, 700, sim_data)
Related