yearmon

0th

Percentile

An Index Class for Monthly Data

"yearmon" is a class for representing monthly data.

Keywords
ts
Usage
yearmon(x)
Arguments
x

numeric (interpreted as being “in years”).

Details

The "yearmon" class is used to represent monthly data. Internally it holds the data as year plus 0 for January, 1/12 for February, 2/12 for March and so on in order that its internal representation is the same as ts class with frequency = 12. If x is not in this format it is rounded via floor(12*x + .0001)/12.

There are coercion methods available for various classes including: default coercion to "yearmon" (which coerces to "numeric" first) and coercions to and from "yearmon" to "Date" (see below), "POSIXct", "POSIXlt", "numeric", "character" and "jul". The last one is from the "tis" package available on CRAN. In the case of as.yearmon.POSIXt the conversion is with respect to GMT. (Use as.yearmon(format(...)) for other time zones.) In the case of as.yearmon.character the format argument uses the same percent code as "Date". These are described in strptime. Unlike "Date" one can specify a year and month with no day. Default formats of "%Y-%m", "%Y-%m-%d" and "%b %Y".

There is an is.numeric method which returns FALSE.

as.Date.yearmon and as.yearmon.yearqtr each has an optional second argument of "frac" which is a number between 0 and 1 inclusive that indicates the fraction of the way through the period that the result represents. The default is 0 which means the beginning of the period.

There is also a date method for as.yearmon usable with objects created with package date.

Sys.yearmon() returns the current year/month and methods for min, max and range are defined (by defining a method for Summary).

A yearmon mean method is also defined.

Value

Returns its argument converted to class yearmon.

See Also

yearqtr, zoo, zooreg, ts

Aliases
  • yearmon
  • as.yearmon
  • as.yearmon.default
  • as.yearmon.numeric
  • as.yearmon.integer
  • as.yearmon.date
  • as.yearmon.dates
  • as.yearmon.Date
  • as.yearmon.timeDate
  • as.yearmon.jul
  • as.yearmon.POSIXt
  • as.yearmon.character
  • as.yearmon.date
  • as.yearmon.factor
  • as.Date
  • as.Date.numeric
  • as.Date.ts
  • as.Date.yearmon
  • as.POSIXct.yearmon
  • as.POSIXlt.yearmon
  • as.list.yearmon
  • as.numeric.yearmon
  • as.character.yearmon
  • as.data.frame.yearmon
  • c.yearmon
  • cycle.yearmon
  • format.yearmon
  • is.numeric.yearmon
  • mean.yearmon
  • print.yearmon
  • range.yearmon
  • summary.yearmon
  • unique.yearmon
  • [.yearmon
  • [[.yearmon
  • MATCH.yearmon
  • Ops.yearmon
  • Summary.yearmon
  • Sys.yearmon
  • -.yearmon
  • xtfrm.yearmon
Examples
# NOT RUN {
x <- as.yearmon(2000 + seq(0, 23)/12)
x

as.yearmon("mar07", "%b%y")
as.yearmon("2007-03-01")
as.yearmon("2007-12")

# returned Date is the fraction of the way through
# the period given by frac (= 0 by default)
as.Date(x)
as.Date(x, frac = 1)
as.POSIXct(x)

# given a Date, x, return the Date of the next Friday
nextfri <- function(x) 7 * ceiling(as.numeric(x - 1)/7) + as.Date(1)

# given a Date, d, return the same Date in the following month
# Note that as.Date.yearmon gives first Date of the month.
d <- as.Date("2005-1-1") + seq(0,90,30)
next.month <- function(d) as.Date(as.yearmon(d) + 1/12) + 
	as.numeric(d - as.Date(as.yearmon(d)))
next.month(d)

# 3rd Friday in last month of the quarter of Date x
as.Date(as.yearmon(as.yearqtr(x)) + 2/12) + 14

z <- zoo(rnorm(24), x, frequency = 12)
z
as.ts(z)

## convert data fram to multivariate monthly "ts" series
## 1.read raw data
Lines.raw <- "ID Date Count
123 20 May 1999 1
123 21 May 1999 3
222 1 Feb 2000 2
222 3 Feb 2000 4
"
DF <- read.table(text = Lines.raw, skip = 1,
 col.names = c("ID", "d", "b", "Y", "Count"))
## 2. fix raw date
DF$yearmon <- as.yearmon(paste(DF$b, DF$Y), "%b %Y")
## 3. aggregate counts over months, convert to zoo and merge over IDs
ag <- function(DF) aggregate(zoo(DF$Count), DF$yearmon, sum)
z <- do.call("merge.zoo", lapply(split(DF, DF$ID), ag))
## 4. convert to "zooreg" and then to "ts"
frequency(z) <- 12
as.ts(z)

xx <- zoo(seq_along(x), x)

## aggregating over year 
as.year <- function(x) as.numeric(floor(as.yearmon(x)))
aggregate(xx, as.year, mean)

# }
Documentation reproduced from package zoo, version 1.8-4, License: GPL-2 | GPL-3

Community examples

Looks like there are no examples yet.