Year.Quarter to Quarter in R- error in as.yearqtr

时间:2016-05-25 14:18:47

标签: r date

> str(qtr)
num 2016

> qtr
[1] 2016.4

> format(as.yearqtr(qtr,"%Y.q"),"Q%q")
[1] "Q2"

我不应该得到" Q4"而不是" Q2"?我哪里错了?感谢。

2 个答案:

答案 0 :(得分:2)

zoo::as.yearqtr()的输入是十进制年。

试试这个:

library(zoo)
x <- 2106 + (0:11)/12
as.yearqtr(x)
 [1] "2106 Q1" "2106 Q1" "2106 Q1" "2106 Q2" "2106 Q2" "2106 Q2" "2106 Q3" "2106 Q3"
 [9] "2106 Q3" "2106 Q4" "2106 Q4" "2106 Q4"

或:

x <- 2106 + (0:3)/4

x
[1] 2106.00 2106.25 2106.50 2106.75

as.yearqtr(x)
[1] "2106 Q1" "2106 Q2" "2106 Q3" "2106 Q4"

答案 1 :(得分:1)

这也有效,可能更容易:

library(zoo)

qtr=2016.4
as.yearqtr(toString(qtr), format="%Y.%q")

[1]"2016 Q4"