如何在 r 中将日期转换为季度并注明年份?
大多数情况下,日期数据仅以日期格式提供,并且不包含年季度信息。当我们要比较季度或进行一些时间序列分析时,就需要年季度信息。利用 zoo 软件包中的 `as.yearqtr` 函数,我们可以转换日期为年季度,如下面的示例所示。
示例
library(zoo) as.yearqtr("2021-01-19",format="%Y-%m-%d")
输出
[1] "2021 Q1"
as.yearqtr("2021-04-19",format="%Y-%m-%d")
[1] "2021 Q2"
as.yearqtr(c("2021-04-19","2020-05-24","2020-11-09"),format="%Y-%m-%d")
[1] "2021 Q2" "2020 Q2" "2020 Q4"
示例
x1<-sample(c("2021-04-19","2020-05-24","2020-11-09"),50,replace=TRUE) x1
输出
[1] "2020-05-24" "2020-05-24" "2021-04-19" "2021-04-19" "2021-04-19" [6] "2021-04-19" "2021-04-19" "2020-11-09" "2020-11-09" "2020-11-09" [11] "2020-05-24" "2021-04-19" "2020-05-24" "2020-05-24" "2020-05-24" [16] "2020-11-09" "2020-05-24" "2020-05-24" "2020-11-09" "2021-04-19" [21] "2020-11-09" "2021-04-19" "2020-05-24" "2021-04-19" "2020-05-24" [26] "2020-05-24" "2020-05-24" "2020-11-09" "2020-05-24" "2020-05-24" [31] "2020-05-24" "2020-11-09" "2020-05-24" "2020-05-24" "2021-04-19" [36] "2021-04-19" "2020-05-24" "2020-11-09" "2021-04-19" "2020-05-24" [41] "2021-04-19" "2020-11-09" "2021-04-19" "2020-11-09" "2020-11-09" [46] "2020-11-09" "2020-05-24" "2020-11-09" "2021-04-19" "2020-05-24"
as.yearqtr(x1,format="%Y-%m-%d")
[1] "2020 Q2" "2020 Q2" "2021 Q2" "2021 Q2" "2021 Q2" "2021 Q2" "2021 Q2" [8] "2020 Q4" "2020 Q4" "2020 Q4" "2020 Q2" "2021 Q2" "2020 Q2" "2020 Q2" [15] "2020 Q2" "2020 Q4" "2020 Q2" "2020 Q2" "2020 Q4" "2021 Q2" "2020 Q4" [22] "2021 Q2" "2020 Q2" "2021 Q2" "2020 Q2" "2020 Q2" "2020 Q2" "2020 Q4" [29] "2020 Q2" "2020 Q2" "2020 Q2" "2020 Q4" "2020 Q2" "2020 Q2" "2021 Q2" [36] "2021 Q2" "2020 Q2" "2020 Q4" "2021 Q2" "2020 Q2" "2021 Q2" "2020 Q4" [43] "2021 Q2" "2020 Q4" "2020 Q4" "2020 Q4" "2020 Q2" "2020 Q4" "2021 Q2" [50] "2020 Q2"
示例
x2<-sample(c("2020-02-05","2020-08-24","2020-06-17","2021-04-19","2020-05-24","2020-11-09"),80,replace=TRUE) x2
输出
[1] "2020-02-05" "2020-08-24" "2020-05-24" "2020-11-09" "2020-02-05" [6] "2020-02-05" "2020-08-24" "2020-11-09" "2020-11-09" "2020-05-24" [11] "2020-02-05" "2020-06-17" "2021-04-19" "2020-06-17" "2020-08-24" [16] "2020-06-17" "2020-08-24" "2021-04-19" "2020-06-17" "2020-08-24" [21] "2020-06-17" "2020-05-24" "2020-02-05" "2020-06-17" "2020-05-24" [26] "2020-05-24" "2020-06-17" "2020-02-05" "2020-11-09" "2020-02-05" [31] "2021-04-19" "2020-05-24" "2020-06-17" "2020-02-05" "2020-02-05" [36] "2020-11-09" "2020-05-24" "2020-06-17" "2020-06-17" "2020-06-17" [41] "2020-11-09" "2020-08-24" "2020-08-24" "2020-06-17" "2021-04-19" [46] "2020-06-17" "2020-11-09" "2020-08-24" "2020-05-24" "2020-08-24" [51] "2020-02-05" "2020-11-09" "2020-11-09" "2020-06-17" "2020-11-09" [56] "2020-02-05" "2020-11-09" "2020-06-17" "2020-05-24" "2021-04-19" [61] "2020-11-09" "2021-04-19" "2020-02-05" "2020-06-17" "2020-06-17" [66] "2021-04-19" "2021-04-19" "2021-04-19" "2020-11-09" "2020-06-17" [71] "2020-11-09" "2021-04-19" "2021-04-19" "2020-02-05" "2020-05-24" [76] "2020-02-05" "2020-02-05" "2020-11-09" "2020-02-05" "2020-06-17"
as.yearqtr(x2,format="%Y-%m-%d")
[1] "2020 Q1" "2020 Q3" "2020 Q2" "2020 Q4" "2020 Q1" "2020 Q1" "2020 Q3" [8] "2020 Q4" "2020 Q4" "2020 Q2" "2020 Q1" "2020 Q2" "2021 Q2" "2020 Q2" [15] "2020 Q3" "2020 Q2" "2020 Q3" "2021 Q2" "2020 Q2" "2020 Q3" "2020 Q2" [22] "2020 Q2" "2020 Q1" "2020 Q2" "2020 Q2" "2020 Q2" "2020 Q2" "2020 Q1" [29] "2020 Q4" "2020 Q1" "2021 Q2" "2020 Q2" "2020 Q2" "2020 Q1" "2020 Q1" [36] "2020 Q4" "2020 Q2" "2020 Q2" "2020 Q2" "2020 Q2" "2020 Q4" "2020 Q3" [43] "2020 Q3" "2020 Q2" "2021 Q2" "2020 Q2" "2020 Q4" "2020 Q3" "2020 Q2" [50] "2020 Q3" "2020 Q1" "2020 Q4" "2020 Q4" "2020 Q2" "2020 Q4" "2020 Q1" [57] "2020 Q4" "2020 Q2" "2020 Q2" "2021 Q2" "2020 Q4" "2021 Q2" "2020 Q1" [64] "2020 Q2" "2020 Q2" "2021 Q2" "2021 Q2" "2021 Q2" "2020 Q4" "2020 Q2" [71] "2020 Q4" "2021 Q2" "2021 Q2" "2020 Q1" "2020 Q2" "2020 Q1" "2020 Q1" [78] "2020 Q4" "2020 Q1" "2020 Q2"
广告