如何在 R 中创建具有随机化的日期向量?
要创建具有随机化的日期向量,我们可以使用样本函数,但需要使用as.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"