I have code that generates a random value from an exponential distribution defined as the following, i.e. with a rate of 4874, and assigns it to a variable tau.
tau <- rexp(n=1,rate=4874)
Sometimes it produces a NaN
value. rexp()
doesn't have an na.rm option, so I used this while loop to re-assign a random deviate if the value produced is NaN
.
while (is.na(tau)) {
tau <- rexp(n=1,rate=4874)
}
If I run it and a NaN
value is produced and assigned initially to tau, and the while loop is entered, it seems unable to stop generating more NaN
values, i.e. I have an infinite loop (the re-assignment line in the while loop just re-assigns a NaN
value each time).
Any ideas? Unfortunately the rexp()
function doesn't allow you to include an na.rm = TRUE
argument. I've been unable to find anything about dealing with NaN
values in R
when they are produced by random sampling of a distribution.