Reputation: 1285
library(lubridate)
date1<-ymd("2021/01/01")
date2<-ymd("2021/01/31")
How can I simulate multiple dates between "2021-01-01"
and "2021-01-31"
, for example ten dates like this:
[1] "2021-01-21" "2021-01-07" "2021-01-09" "2021-01-18" "2021-01-02" "2021-01-13" "2021-01-24" "2021-01-30" "2021-01-11" "2021-01-25"
Upvotes: 2
Views: 38
Reputation: 26695
You can use seq()
, e.g.
library(lubridate)
date1<-ymd("2021/01/01")
date2<-ymd("2021/01/31")
seq(date1, date2, length.out = 10)
>[1] "2021-01-01" "2021-01-04" "2021-01-07" "2021-01-11"
>[5] "2021-01-14" "2021-01-17" "2021-01-21" "2021-01-24"
>[9] "2021-01-27" "2021-01-31"
If you want 10 random dates:
library(lubridate)
date1<-ymd("2021/01/01")
date2<-ymd("2021/01/31")
dates <- seq(date1, date2, 1)
sample(dates, 10)
>[1] "2021-01-08" "2021-01-03" "2021-01-20" "2021-01-27"
>[5] "2021-01-02" "2021-01-17" "2021-01-19" "2021-01-30"
>[9] "2021-01-11" "2021-01-05"
Upvotes: 1