如何在 R 中创建具有随机性的日期向量?


要创建具有随机性的日期向量,我们可以使用 sample 函数,但日期需要作为 Date 函数读取。例如,如果我们有 2 个日期,例如 2021-01-01 和 2021-02-02,则可以使用以下命令创建包含这三个日期的随机向量,即 −

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

示例 1

 在线演示

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

输出

[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"

示例 2

 在线演示

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

输出

[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"

示例 3

 在线演示

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

输出

[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"

示例 4

 在线演示

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

输出

[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"

更新日期: 06-Mar-2021

178 次浏览

开启您的 职业生涯

完成该课程即可获得认证

开始
广告