5

i have a tsv where there is a date field (named "trim")in quarter format

 trim
 1992-4
 1993-1
 ...

When i load the file as data frame, R imports that field as character.

i can't convert it, i get NA only, even if I try:

 df$trim <- as.Date(df$trim, format="%Y-%q")

or loading the zoo package and send this command:

as.yearqtr(df$trim, format="%Y-%q")

Any idea?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
andriatz
  • 622
  • 2
  • 9
  • 22
  • `%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. – Benjamin Sep 03 '15 at 10:21
  • 2
    @Benjamin With `zoo` package, you can store year-quarter object as `yearqtr`. But it in the format `YYYY Qx`. –  Sep 03 '15 at 10:23
  • I stand corrected. Although, based on the documentation of `?zoo::yearqtr`, there doesn't appear to be a straightforward way to convert these character strings to `yearqtr` objects. – Benjamin Sep 03 '15 at 10:34

3 Answers3

5

This converts a character string to "yearqtr" class and "Date" class (first of quarter) and "Date" class (last of quarter):

library(zoo)

as.yearqtr("1992-4")
## [1] "1992 Q4"

as.Date(as.yearqtr("1992-4"))
## [1] "1992-10-01"

as.Date(as.yearqtr("1992-4"), frac = 1)
## [1] "1992-12-31"

Note that zoo has scale_x_yearqtr for defining a ggplot2 X axis suitable for "yearqtr" class.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
3

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.

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • thank you, so i can't use the quarter format date to plot something. With ggplot2 i must work on scales and labels – andriatz Sep 03 '15 at 10:50
  • Check out the comment from @Pascal ... baseR doesn't have a quarter date object although other packages (e.g. `zoo`) might have this. – Tim Biegeleisen Sep 03 '15 at 10:55
3

You can use lubridate. It can be loaded using tidyverse.

library(tidyverse)
yq('1992-4')
str(yq('1992-4'))

parse_date_time('1992-4',orders = "Yq")
parse_date_time('1992-4',orders = "%Y%q")

[1] "1992-10-01"
Date[1:1], format: "1992-10-01"
[1] "1992-10-01 UTC"
[1] "1992-10-01 UTC"
Harlan Nelson
  • 1,394
  • 1
  • 10
  • 22