Generating Random Dates
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
Solution 1:
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)
Solution 2:
Several ways:
Start with a single
Date
object, and just add result fromsample()
Start with a sequence of
Date
objects, andsample()
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>
Solution 3:
td = as.Date('2000/01/01') - as.Date('1999/01/01')
as.Date('1999/01/01') + sample(0:td, 12)