How to create a date vector with randomization in R?


To create a date vector with randomization, we can use sample function but the dates need to be read with as.Date function. For example, if we have 2 dates say 2021-01-01 and 2021-02-02 then a vector with randomization of these three dates can be created by using the command −

sample(c(as.Date("2021-01-01"),as.Date("2021-02-02")),100,replace=TRUE)

Example1

 Live Demo

Date1<-sample(c(as.Date("2020-05-01"),as.Date("2020-05-21"),as.Date("2020-06-18"),as.Date("2020-06-20")),100,replace=TRUE)
Date1

Output

[1] "2020-06-20" "2020-06-18" "2020-06-20" "2020-05-01" "2020-05-21"
[6] "2020-06-20" "2020-05-01" "2020-06-20" "2020-06-18" "2020-06-20"
[11] "2020-05-21" "2020-06-18" "2020-06-18" "2020-05-21" "2020-06-18"
[16] "2020-06-20" "2020-06-18" "2020-05-21" "2020-05-21" "2020-06-18"
[21] "2020-06-20" "2020-06-18" "2020-06-18" "2020-05-01" "2020-05-21"
[26] "2020-05-01" "2020-06-20" "2020-05-01" "2020-05-21" "2020-06-18"
[31] "2020-05-01" "2020-06-18" "2020-06-20" "2020-05-01" "2020-05-01"
[36] "2020-05-21" "2020-06-18" "2020-05-21" "2020-06-18" "2020-05-21"
[41] "2020-05-01" "2020-06-18" "2020-06-18" "2020-05-21" "2020-06-20"
[46] "2020-05-01" "2020-05-01" "2020-05-21" "2020-06-18" "2020-06-18"
[51] "2020-05-01" "2020-06-20" "2020-05-01" "2020-05-21" "2020-06-18"
[56] "2020-06-20" "2020-05-21" "2020-05-21" "2020-05-21" "2020-05-01"
[61] "2020-06-20" "2020-05-21" "2020-05-21" "2020-05-21" "2020-05-21"
[66] "2020-06-18" "2020-06-20" "2020-06-20" "2020-05-21" "2020-06-18"
[71] "2020-06-18" "2020-06-20" "2020-06-18" "2020-06-18" "2020-06-18"
[76] "2020-05-01" "2020-05-01" "2020-06-20" "2020-06-20" "2020-05-21"
[81] "2020-05-21" "2020-05-21" "2020-06-20" "2020-05-21" "2020-06-18"
[86] "2020-06-18" "2020-06-20" "2020-05-01" "2020-05-01" "2020-05-01"
[91] "2020-05-01" "2020-05-01" "2020-05-21" "2020-05-21" "2020-05-21"
[96] "2020-06-18" "2020-06-18" "2020-06-20" "2020-05-01" "2020-05-21"

Example2

 Live Demo

Date2<-sample(c(as.Date("2020-01-01"),as.Date("2020-04-15"),as.Date("2020-06-03"),as.Date("2020-02-25"),as.Date("2020-10-09")),100,replace=TRUE)
Date2

Output

[1] "2020-10-09" "2020-04-15" "2020-02-25" "2020-10-09" "2020-01-01"
[6] "2020-10-09" "2020-06-03" "2020-02-25" "2020-01-01" "2020-06-03"
[11] "2020-01-01" "2020-10-09" "2020-02-25" "2020-04-15" "2020-01-01"
[16] "2020-01-01" "2020-01-01" "2020-10-09" "2020-04-15" "2020-10-09"
[21] "2020-01-01" "2020-01-01" "2020-04-15" "2020-10-09" "2020-10-09"
[26] "2020-06-03" "2020-10-09" "2020-04-15" "2020-02-25" "2020-02-25"
[31] "2020-04-15" "2020-10-09" "2020-06-03" "2020-06-03" "2020-06-03"
[36] "2020-04-15" "2020-02-25" "2020-10-09" "2020-04-15" "2020-06-03"
[41] "2020-04-15" "2020-01-01" "2020-04-15" "2020-02-25" "2020-06-03"
[46] "2020-02-25" "2020-06-03" "2020-01-01" "2020-02-25" "2020-10-09"
[51] "2020-02-25" "2020-02-25" "2020-02-25" "2020-02-25" "2020-01-01"
[56] "2020-01-01" "2020-04-15" "2020-10-09" "2020-10-09" "2020-10-09"
[61] "2020-02-25" "2020-06-03" "2020-06-03" "2020-01-01" "2020-10-09"
[66] "2020-06-03" "2020-04-15" "2020-10-09" "2020-06-03" "2020-01-01"
[71] "2020-01-01" "2020-10-09" "2020-02-25" "2020-06-03" "2020-10-09"
[76] "2020-10-09" "2020-04-15" "2020-06-03" "2020-01-01" "2020-01-01"
[81] "2020-06-03" "2020-02-25" "2020-04-15" "2020-04-15" "2020-06-03"
[86] "2020-02-25" "2020-06-03" "2020-02-25" "2020-02-25" "2020-06-03"
[91] "2020-01-01" "2020-02-25" "2020-04-15" "2020-04-15" "2020-06-03"
[96] "2020-04-15" "2020-04-15" "2020-10-09" "2020-04-15" "2020-10-09"

Example3

 Live Demo

Date3<-sample(c(as.Date("2004-01-11"),as.Date("2003-04-15"),as.Date("2008-06-13"),as.Date("2002-02-25"),as.Date("2020-12-09")),100,replace=TRUE)
Date3

Output

[1] "2020-12-09" "2004-01-11" "2003-04-15" "2004-01-11" "2020-12-09"
[6] "2004-01-11" "2008-06-13" "2008-06-13" "2020-12-09" "2003-04-15"
[11] "2002-02-25" "2008-06-13" "2004-01-11" "2004-01-11" "2003-04-15"
[16] "2004-01-11" "2008-06-13" "2008-06-13" "2003-04-15" "2003-04-15"
[21] "2008-06-13" "2004-01-11" "2004-01-11" "2008-06-13" "2020-12-09"
[26] "2004-01-11" "2020-12-09" "2002-02-25" "2004-01-11" "2004-01-11"
[31] "2020-12-09" "2020-12-09" "2004-01-11" "2020-12-09" "2020-12-09"
[36] "2003-04-15" "2002-02-25" "2002-02-25" "2020-12-09" "2002-02-25"
[41] "2004-01-11" "2008-06-13" "2002-02-25" "2004-01-11" "2004-01-11"
[46] "2008-06-13" "2004-01-11" "2020-12-09" "2008-06-13" "2004-01-11"
[51] "2004-01-11" "2020-12-09" "2004-01-11" "2002-02-25" "2003-04-15"
[56] "2004-01-11" "2003-04-15" "2004-01-11" "2004-01-11" "2008-06-13"
[61] "2008-06-13" "2008-06-13" "2004-01-11" "2003-04-15" "2003-04-15"
[66] "2002-02-25" "2004-01-11" "2020-12-09" "2004-01-11" "2008-06-13"
[71] "2008-06-13" "2002-02-25" "2002-02-25" "2004-01-11" "2020-12-09"
[76] "2020-12-09" "2002-02-25" "2003-04-15" "2020-12-09" "2002-02-25"
[81] "2008-06-13" "2003-04-15" "2020-12-09" "2008-06-13" "2003-04-15"
[86] "2002-02-25" "2003-04-15" "2020-12-09" "2002-02-25" "2002-02-25"
[91] "2003-04-15" "2004-01-11" "2020-12-09" "2008-06-13" "2004-01-11"
[96] "2008-06-13" "2020-12-09" "2004-01-11" "2008-06-13" "2008-06-13"

Example4

 Live Demo

Date4<-sample(c(as.Date("2021-01-11"),as.Date("2021-01-15"),as.Date("2021-02-13"),as.Date("2021-02-05"),as.Date("2021-02-09")),100,replace=TRUE)
Date4

Output

[1] "2021-01-15" "2021-02-09" "2021-02-13" "2021-01-11" "2021-02-05"
[6] "2021-02-13" "2021-02-13" "2021-01-15" "2021-01-15" "2021-02-09"
[11] "2021-01-11" "2021-01-15" "2021-01-11" "2021-01-15" "2021-01-11"
[16] "2021-02-13" "2021-01-15" "2021-01-15" "2021-01-15" "2021-02-09"
[21] "2021-02-09" "2021-01-15" "2021-02-13" "2021-02-13" "2021-02-09"
[26] "2021-01-15" "2021-02-09" "2021-02-09" "2021-02-09" "2021-02-09"
[31] "2021-01-15" "2021-02-05" "2021-02-05" "2021-02-09" "2021-02-05"
[36] "2021-01-15" "2021-01-15" "2021-01-11" "2021-01-15" "2021-01-11"
[41] "2021-02-05" "2021-01-11" "2021-01-11" "2021-01-15" "2021-02-05"
[46] "2021-01-11" "2021-02-05" "2021-01-15" "2021-02-05" "2021-02-13"
[51] "2021-02-13" "2021-01-11" "2021-01-11" "2021-02-09" "2021-02-05"
[56] "2021-02-05" "2021-01-11" "2021-01-11" "2021-02-09" "2021-01-11"
[61] "2021-01-15" "2021-02-05" "2021-01-11" "2021-02-05" "2021-02-13"
[66] "2021-01-11" "2021-01-11" "2021-02-13" "2021-01-15" "2021-02-09"
[71] "2021-02-13" "2021-01-11" "2021-02-05" "2021-02-13" "2021-02-13"
[76] "2021-02-13" "2021-02-13" "2021-01-15" "2021-02-09" "2021-02-13"
[81] "2021-01-15" "2021-02-09" "2021-01-11" "2021-02-05" "2021-01-11"
[86] "2021-02-09" "2021-01-11" "2021-01-15" "2021-02-13" "2021-02-13"
[91] "2021-01-11" "2021-01-15" "2021-02-13" "2021-01-11" "2021-02-09"
[96] "2021-02-05" "2021-02-05" "2021-02-13" "2021-02-09" "2021-02-13"

Updated on: 06-Mar-2021

141 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements