如何在R中将日期或日期向量转换为POSIXct?
要将日期或日期向量转换为POSIXct,我们可以使用as.POSIXct函数,但我们还需要在函数内部传递适当的日期格式。例如,如果我们有一个日期“ 2020-11-14”,则可以使用as.POSIXct(“ 2020-11-14”,format =“%Y-%m-%d”)将其转换为POSIXct。
例1
> date1<-"2020-04-01"> as.POSIXct(date1,format="%Y-%m-%d")
输出结果
[1] "2020-04-01 IST"
例2
> date2<-sample(c("2020-04-01","2020-10-01","2020-02-12","2020-04-11","2020-01-21","2020-04-27","2020-03-31","2020-05-01","2020-05-11","2020-06-01"),60,replace=TRUE)> date2
输出结果
[1] "2020-02-12" "2020-06-01" "2020-04-01" "2020-05-01" "2020-01-21"[6] "2020-01-21" "2020-06-01" "2020-04-27" "2020-05-11" "2020-06-01"
[11] "2020-01-21" "2020-03-31" "2020-05-01" "2020-02-12" "2020-01-21"
[16] "2020-05-01" "2020-03-31" "2020-04-01" "2020-05-01" "2020-01-21"
[21] "2020-05-01" "2020-04-11" "2020-05-11" "2020-04-01" "2020-03-31"
[26] "2020-04-11" "2020-04-01" "2020-03-31" "2020-04-01" "2020-04-11"
[31] "2020-05-11" "2020-06-01" "2020-03-31" "2020-04-27" "2020-01-21"
[36] "2020-01-21" "2020-04-01" "2020-06-01" "2020-05-01" "2020-10-01"
[41] "2020-04-27" "2020-01-21" "2020-10-01" "2020-01-21" "2020-04-01"
[46] "2020-04-11" "2020-10-01" "2020-10-01" "2020-05-11" "2020-02-12"
[51] "2020-05-01" "2020-05-01" "2020-01-21" "2020-01-21" "2020-02-12"
[56] "2020-05-01" "2020-02-12" "2020-02-12" "2020-04-11" "2020-10-01"
示例
> as.POSIXct(date2,format="%Y-%m-%d")
输出结果
[1] "2020-02-12 IST" "2020-06-01 IST" "2020-04-01 IST" "2020-05-01 IST"[5] "2020-01-21 IST" "2020-01-21 IST" "2020-06-01 IST" "2020-04-27 IST"
[9] "2020-05-11 IST" "2020-06-01 IST" "2020-01-21 IST" "2020-03-31 IST"
[13] "2020-05-01 IST" "2020-02-12 IST" "2020-01-21 IST" "2020-05-01 IST"
[17] "2020-03-31 IST" "2020-04-01 IST" "2020-05-01 IST" "2020-01-21 IST"
[21] "2020-05-01 IST" "2020-04-11 IST" "2020-05-11 IST" "2020-04-01 IST"
[25] "2020-03-31 IST" "2020-04-11 IST" "2020-04-01 IST" "2020-03-31 IST"
[29] "2020-04-01 IST" "2020-04-11 IST" "2020-05-11 IST" "2020-06-01 IST"
[33] "2020-03-31 IST" "2020-04-27 IST" "2020-01-21 IST" "2020-01-21 IST"
[37] "2020-04-01 IST" "2020-06-01 IST" "2020-05-01 IST" "2020-10-01 IST"
[41] "2020-04-27 IST" "2020-01-21 IST" "2020-10-01 IST" "2020-01-21 IST"
[45] "2020-04-01 IST" "2020-04-11 IST" "2020-10-01 IST" "2020-10-01 IST"
[49] "2020-05-11 IST" "2020-02-12 IST" "2020-05-01 IST" "2020-05-01 IST"
[53] "2020-01-21 IST" "2020-01-21 IST" "2020-02-12 IST" "2020-05-01 IST"
[57] "2020-02-12 IST" "2020-02-12 IST" "2020-04-11 IST" "2020-10-01 IST"
例子3
> date3<-rep(c("2020-04-01","2020-10-01","2020-02-12","2020-04-11","2020-01-21","2020-04-27","2020-03-31","2020-05-01","2020-05-11","2020-06-01"),10)> date3
输出结果
[1] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"[6] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[11] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[16] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[21] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[26] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[31] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[36] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[41] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[46] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[51] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[56] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[61] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[66] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[71] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[76] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[81] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[86] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
[91] "2020-04-01" "2020-10-01" "2020-02-12" "2020-04-11" "2020-01-21"
[96] "2020-04-27" "2020-03-31" "2020-05-01" "2020-05-11" "2020-06-01"
示例
> as.POSIXct(date3,format="%Y-%m-%d")
输出结果
[1] "2020-04-01 IST" "2020-10-01 IST" "2020-02-12 IST" "2020-04-11 IST"[5] "2020-01-21 IST" "2020-04-27 IST" "2020-03-31 IST" "2020-05-01 IST"
[9] "2020-05-11 IST" "2020-06-01 IST" "2020-04-01 IST" "2020-10-01 IST"
[13] "2020-02-12 IST" "2020-04-11 IST" "2020-01-21 IST" "2020-04-27 IST"
[17] "2020-03-31 IST" "2020-05-01 IST" "2020-05-11 IST" "2020-06-01 IST"
[21] "2020-04-01 IST" "2020-10-01 IST" "2020-02-12 IST" "2020-04-11 IST"
[25] "2020-01-21 IST" "2020-04-27 IST" "2020-03-31 IST" "2020-05-01 IST"
[29] "2020-05-11 IST" "2020-06-01 IST" "2020-04-01 IST" "2020-10-01 IST"
[33] "2020-02-12 IST" "2020-04-11 IST" "2020-01-21 IST" "2020-04-27 IST"
[37] "2020-03-31 IST" "2020-05-01 IST" "2020-05-11 IST" "2020-06-01 IST"
[41] "2020-04-01 IST" "2020-10-01 IST" "2020-02-12 IST" "2020-04-11 IST"
[45] "2020-01-21 IST" "2020-04-27 IST" "2020-03-31 IST" "2020-05-01 IST"
[49] "2020-05-11 IST" "2020-06-01 IST" "2020-04-01 IST" "2020-10-01 IST"
[53] "2020-02-12 IST" "2020-04-11 IST" "2020-01-21 IST" "2020-04-27 IST"
[57] "2020-03-31 IST" "2020-05-01 IST" "2020-05-11 IST" "2020-06-01 IST"
[61] "2020-04-01 IST" "2020-10-01 IST" "2020-02-12 IST" "2020-04-11 IST"
[65] "2020-01-21 IST" "2020-04-27 IST" "2020-03-31 IST" "2020-05-01 IST"
[69] "2020-05-11 IST" "2020-06-01 IST" "2020-04-01 IST" "2020-10-01 IST"
[73] "2020-02-12 IST" "2020-04-11 IST" "2020-01-21 IST" "2020-04-27 IST"
[77] "2020-03-31 IST" "2020-05-01 IST" "2020-05-11 IST" "2020-06-01 IST"
[81] "2020-04-01 IST" "2020-10-01 IST" "2020-02-12 IST" "2020-04-11 IST"
[85] "2020-01-21 IST" "2020-04-27 IST" "2020-03-31 IST" "2020-05-01 IST"
[89] "2020-05-11 IST" "2020-06-01 IST" "2020-04-01 IST" "2020-10-01 IST"
[93] "2020-02-12 IST" "2020-04-11 IST" "2020-01-21 IST" "2020-04-27 IST"
[97] "2020-03-31 IST" "2020-05-01 IST" "2020-05-11 IST" "2020-06-01 IST"
以上是 如何在R中将日期或日期向量转换为POSIXct? 的全部内容, 来源链接: utcz.com/z/356831.html