Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read a date series of quarterly data into R?

Tags:

r

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?

like image 696
Vivi Avatar asked Dec 15 '11 03:12

Vivi


People also ask

How do I extract a date from a quarter in R?

If you want to get a quarter from date in R as a number, you can use the quarter function from the lubridate package. The function will extract only a quarter number, and you can use that as it is.

How do I declare a time series data in R?

Creating a time seriesThe ts() function will convert a numeric vector into an R time series object. The format is ts(vector, start=, end=, frequency=) where start and end are the times of the first and last observation and frequency is the number of observations per unit time (1=annual, 4=quartly, 12=monthly, etc.).

How do I view dates in R?

In R programming, if you use Sys. Date() function, it will give you the system date. You don't need to add an argument inside the parentheses to this function.

How do I code a date in R?

To create a Date object from a simple character string in R, you can use the as. Date() function. The character string has to obey a format that can be defined using a set of symbols (the examples correspond to 13 January, 1982): %Y : 4-digit year (1982)


1 Answers

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"
like image 120
G. Grothendieck Avatar answered Oct 04 '22 00:10

G. Grothendieck