如何在R中将日期转换为季度和年份?
在大多数情况下,日期数据仅以日期格式提供,而没有有关该季度的信息。当我们要比较季度或进行一些时间序列分析时,需要一年的季度。可以使用as.yearqtrZoo包中的函数将日期转换为季度和年份,如以下示例所示。
示例
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"
以上是 如何在R中将日期转换为季度和年份? 的全部内容, 来源链接: utcz.com/z/359621.html