If you insist on using the base R as.Date()
function, then one option for you to consider is mapping the quarters to months. In the below code, I map the quarter to the first month occurring in that quarter. Then I tag on "01"
for the first day in that month, and afterward convert to a date.
trim <- c("1992-4", "1993-1")
trim <- gsub("-1", "-01", trim) # map first quarter to January
trim <- gsub("-2", "-04", trim) # map second quarter to April
trim <- gsub("-3", "-07", trim) # map third quarter to July
trim <- gsub("-4", "-10", trim) # map fourth quarter October
trim <- paste(trim, "-01", sep="") # add first day of the month
trim <- as.Date(trim, "%Y-%m-%d") # convert to date
> trim
[1] "1992-10-01" "1993-01-01"
P.S. This question is sort of a duplicate of this SO post. But it's different enough to merit a new answer IMO.
%q
is not a format listed in?strptime
. I don't think you can store the year-quarter date object. However, storing it as a factor should be just fine, as the way in which this is generated will still sort as desired.zoo
package, you can store year-quarter object asyearqtr
. But it in the formatYYYY Qx
.?zoo::yearqtr
, there doesn't appear to be a straightforward way to convert these character strings toyearqtr
objects.