An Index Class for Quarterly Data
yearqtr.Rd"yearqtr" is a class for representing quarterly data.
Usage
yearqtr(x)
as.yearqtr(x, ...)
# S3 method for class 'character'
as.yearqtr(x, format, ...)
# S3 method for class 'yearqtr'
format(x, format = "%Y Q%q", ...)Arguments
- x
for
yearqtra numeric (interpreted as being “in years”). Foras.yearqtranother date class object. For the"yearqtr"method offormatan object of class"yearqtr"or if called asformat.yearqtrthen an object with anas.yearqtrmethod that can be coerced to"yearqtr".- format
character string specifying format. For coercing to
"yearqtr"from character:"%Y"and"%q"have to be specified. For formatting an existing"yearqtr":"%C","%Y","%y"and"%q", if present, are replaced with the century, year, last two digits of the year, and quarter (i.e. a number between 1 and 4), respectively.- ...
arguments passed ot other methods.
Details
The "yearqtr" class is used to represent quarterly data. Internally it holds
the data as year plus 0 for Quarter 1, 1/4 for Quarter 2
and so on in order that its internal representation is the same as
ts class with frequency = 4. If x is not in this
format it is rounded via floor(4*x + .0001)/4.
as.yearqtr.character uses a default format of "%Y Q%q",
"%Y q%q" or "%Y-%q" according to whichever matches.
%q accepts the numbers 1-4 (possibly with leading zeros). Due to
this %q does not match to single digits only and consequently
formats such as as.yearqtr("Q12000", "Q%q%Y") are ambiguous and
do not work (i.e., result in NA).
There are coercion methods available for various classes including:
default coercion to "yearqtr" (which coerces to "numeric" first)
and coercion from "yearqtr" to "Date" (see below), "POSIXct",
"POSIXlt", "numeric", "character" and "jul".
The last one is from the frame package on CRAN.
There is an is.numeric method which returns FALSE.
There is also a date method for as.yearqtr usable with objects
created with package date.
Sys.yearqtr() returns the current year/month and methods for
min, max and range are defined (by defining
a method for Summary).
A yearqtr mean method is also defined.
Certain methods support a frac argument. See yearmon.
Value
yearqtr and as.yearqtr return the first argument converted to
class yearqtr.
The format method returns a character string representation of
its argument first argument.
Examples
Sys.setenv(TZ = "GMT")
x <- as.yearqtr(2000 + seq(0, 7)/4)
x
#> [1] "2000 Q1" "2000 Q2" "2000 Q3" "2000 Q4" "2001 Q1" "2001 Q2" "2001 Q3"
#> [8] "2001 Q4"
format(x, "%Y Quarter %q")
#> [1] "2000 Quarter 1" "2000 Quarter 2" "2000 Quarter 3" "2000 Quarter 4"
#> [5] "2001 Quarter 1" "2001 Quarter 2" "2001 Quarter 3" "2001 Quarter 4"
as.yearqtr("2001 Q2")
#> [1] "2001 Q2"
as.yearqtr("2001 q2") # same
#> [1] "2001 Q2"
as.yearqtr("2001-2") # same
#> [1] "2001 Q2"
# returned Date is the fraction of the way through
# the period given by frac (= 0 by default)
dd <- as.Date(x)
format.yearqtr(dd)
#> [1] "2000 Q1" "2000 Q2" "2000 Q3" "2000 Q4" "2001 Q1" "2001 Q2" "2001 Q3"
#> [8] "2001 Q4"
as.Date(x, frac = 1)
#> [1] "2000-03-31" "2000-06-30" "2000-09-30" "2000-12-31" "2001-03-31"
#> [6] "2001-06-30" "2001-09-30" "2001-12-31"
as.POSIXct(x)
#> [1] "2000-01-01 GMT" "2000-04-01 GMT" "2000-07-01 GMT" "2000-10-01 GMT"
#> [5] "2001-01-01 GMT" "2001-04-01 GMT" "2001-07-01 GMT" "2001-10-01 GMT"
suppressWarnings(RNGversion("3.5.0"))
set.seed(1)
zz <- zoo(rnorm(8), x, frequency = 4)
zz
#> 2000 Q1 2000 Q2 2000 Q3 2000 Q4 2001 Q1 2001 Q2 2001 Q3
#> -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 -0.8204684 0.4874291
#> 2001 Q4
#> 0.7383247
as.ts(zz)
#> Qtr1 Qtr2 Qtr3 Qtr4
#> 2000 -0.6264538 0.1836433 -0.8356286 1.5952808
#> 2001 0.3295078 -0.8204684 0.4874291 0.7383247