Reputation: 4218
I have a dataset, the first column of which is the date in the format Year-Quarter, like the following
1959-I
1959-II
1959-III
1959-IV
1960-I
1960-II
1960-III
1960-IV
I have imported the dataset into R, but I don't know how to convert this to Date
format using the as.Date
function, since there doesn't appear to be a conversion specification for quarters and I have just started learning R.
The best I can think of is something like
#extract the year
> dates <- substring(data$X,1,4)
> dates[1:8]
[1] "1959" "1959" "1959" "1959" "1960" "1960" "1960" "1960"
> dates <- as.numeric(dates)
#extract the quarter
> quarters <- substring(data$X,6)
> quarters[1:10]
[1] "I " "II " "III " "IV " "I " "II " "III " "IV "
but this cannot possibly be the best way of doing this, and it still leaves me the problem of how to deal with my series quarters
. To make matters worse, there is a blank character at the end of each character I don't know how to deal with because there are 3 different "lenghts" for the quarters strings.
Another option, of course, would be to fabricate my own quarters series, with something like
quarters <- rep(c(1,2,3,4),dates[length(dates)]-dates[1])
(I am quite proud of myself for having written this last line!).
So my question: is there a built-in way to import quarterly data in the format I have in R, and if not, any other format (still for quarterly data)? Any suggestions on the best way to proceed?
Upvotes: 10
Views: 21950
Reputation: 269644
Suppose we have data 1:8 which corresponds to the 8 quarters you mention. Then we can do this:
ts(1:8, start = c(1959, 1), frequency = 4)
## Qtr1 Qtr2 Qtr3 Qtr4
## 1959 1 2 3 4
## 1960 5 6 7 8
Also the zoo package has the "yearqtr"
class:
library(zoo)
z <- zooreg(1:8, start = as.yearqtr("1959-1"), frequency = 4)
z
## 1959 Q1 1959 Q2 1959 Q3 1959 Q4 1960 Q1 1960 Q2 1960 Q3 1960 Q4
## 1 2 3 4 5 6 7 8
If we really did have a vector of such quarters that are not necessarily consecutive:
dt <- c("1959-I", "1959-II", "1959-III", "1959-IV", "1960-I", "1960-II",
"1960-III", "1960-IV")
we could convert them using gsubfn
:
library(gsubfn)
g <- gsubfn("I.*", list(`I` = 1, `II` = 2, `III` = 3, `IV` = 4), dt)
g
## [1] "1959-1" "1959-2" "1959-3" "1959-4" "1960-1" "1960-2" "1960-3" "1960-4"
as.yearqtr(g)
## [1] "1959 Q1" "1959 Q2" "1959 Q3" "1959 Q4" "1960 Q1" "1960 Q2" "1960 Q3"
## [8] "1960 Q4"
Upvotes: 19