Monday, 12 August 2013

How to scratch the year and quarter value from quarterly dates in R

How to scratch the year and quarter value from quarterly dates in R

I wanna scratch the year value and quarter value for converting to a
time-series data.
quaterly_data <- c(30.53,36.36,40.73,45.19,
55.67,63.19,72.2,81.19,
100.37,120.32,141.64,143.89,
154.14,164.5,174.32,189.26,
224.13,239.35,255.95,277.23)
#quarterly dates
quarterly_dates <- as.yearqtr(seq(as.Date("2002/3/1"),
as.Date("2006/12/1"), by = "3 months"))
The dates look like this:
[1] "2002 Q1" "2002 Q2" "2002 Q3" "2002 Q4" "2003 Q1" "2003 Q2" "2003 Q3"
"2003 Q4" "2004 Q1" "2004 Q2"
[11] "2004 Q3" "2004 Q4" "2005 Q1" "2005 Q2" "2005 Q3" "2005 Q4" "2006 Q1"
"2006 Q2" "2006 Q3" "2006 Q4"
I wanna write a function to scratch out the first element "2002 Q1", and
put it into myts <- ts(quaterly_data,start = c(2002, 1), frequency = 4).
But I don't know how to do that.

No comments:

Post a Comment