yearmon {zoo} | R Documentation |
"yearmon"
is a class for representing monthly data.
yearmon(x)
x |
numeric (interpreted as being “in years”). |
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 coercion from "yearmon"
to "Date"
(see below), "POSIXct"
,
"POSIXlt"
, "numeric"
, "character"
. In the case of
as.yearmon.character
the format
argument is the same as for
"Date"
plus one can have a format specifying a year and month but
no day.
Returns its argument converted to class yearmon
.
x <- yearmon(2000 + seq(0, 23)/12) x as.yearmon("mar07", "%b%y") as.yearmon("2007-03-01") # 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) 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(textConnection(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)