How can I generate a set of 12 random dates within a specific date range?
I thought the following would work:
sample(as.Date(1999/01/01), as.Date(2000/01/01),12)
But the result looks like a random set of numbers?
Thank you
To generate random dates between two dates, you can use the RANDBETWEEN function, together with the DATE function. This formula is then copied down from B5 to B11. The result is random dates between Jan 1, 2016 and Dec 31, 2016 (random dates in the year 2016).
1. RAND()*(13-11)/24+11/24: This formula will return a random value in 24hr time format between 11 o'clock and 13 o'clock. 2. TEXT(RAND()*(15-11)/24+11/24, “HH:MM: SS”): This formula will return the values returned by the RAND function in standard HH: MM: SS format.
seq
has a method for class Date
which works for this:
sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 12)
Several ways:
Start with a single Date
object, and just add result from sample()
Start with a sequence of Date
objects, and sample()
it.
Here is 1:
R> set.seed(42) R> res <- Sys.Date() + sort(sample(1:10, 3)) R> res [1] "2014-02-04" "2014-02-10" "2014-02-11" R>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With