Demet
Demet

Reputation: 105

How to convert numeric date variable into quarterly date?

I have a panel dataset, and the first column of which is the date in the format Year-quarter in Stata, like the following:

quarter id
1993q1  1
1993q2  1
1993q3  1
1993q4  1
1994q1  1
1994q2  1
1994q3  1
1994q4  1
1995q1  1
1995q2  1
1995q3  1
1995q4  1

I have imported the dataset from Stata into R, but I don't know how to convert this to the Date format using as.Date function.(I have just started learning R) The Date looks like the following in R:

quarter
137
138
139
140
141
142
143
144
145
146
147

So my question: is there a built-in way to import quarterly data in the format I have in Stata, and if not, how can I convert this numeric date into quarterly in R? Any suggestions?

Upvotes: 0

Views: 1171

Answers (2)

Anthony Tatum
Anthony Tatum

Reputation: 23

Try:

library(zoo)
quarter <- c("1993q1", "1993q2")
as.yearqtr(quarter)
## [1] "1993 Q1" "1993 Q2"

Also note the percent codes in ?strftime and in ?as.yearqtr

Upvotes: 0

cryo111
cryo111

Reputation: 4474

You can use as.yearqtr from the zoo package. For the string format:

library(zoo)
as.yearqtr("1993q1",format="%Yq%q")
#[1] "1993 Q1"

For the number format:

#origin seems to be 1958 Q4
start_val=1958.75
#137 is the first value in your list
as.yearqtr(start_val+137/4)
#[1] "1993 Q1"

Upvotes: 3

Related Questions