library("zoo")
Sys.setenv(TZ = "GMT")
x <- as.yearmon(2000 + seq(0, 23)/12)
x [1] "Jan 2000" "Feb 2000" "Mar 2000" "Apr 2000" "May 2000" "Jun 2000"
[7] "Jul 2000" "Aug 2000" "Sep 2000" "Oct 2000" "Nov 2000" "Dec 2000"
[13] "Jan 2001" "Feb 2001" "Mar 2001" "Apr 2001" "May 2001" "Jun 2001"
[19] "Jul 2001" "Aug 2001" "Sep 2001" "Oct 2001" "Nov 2001" "Dec 2001"
as.yearmon("mar07", "%b%y")[1] "Mar 2007"
as.yearmon("2007-03-01")[1] "Mar 2007"
as.yearmon("2007-12")[1] "Dec 2007"
# returned Date is the fraction of the way through
# the period given by frac (= 0 by default)
as.Date(x) [1] "2000-01-01" "2000-02-01" "2000-03-01" "2000-04-01" "2000-05-01"
[6] "2000-06-01" "2000-07-01" "2000-08-01" "2000-09-01" "2000-10-01"
[11] "2000-11-01" "2000-12-01" "2001-01-01" "2001-02-01" "2001-03-01"
[16] "2001-04-01" "2001-05-01" "2001-06-01" "2001-07-01" "2001-08-01"
[21] "2001-09-01" "2001-10-01" "2001-11-01" "2001-12-01"
as.Date(x, frac = 1) [1] "2000-01-31" "2000-02-29" "2000-03-31" "2000-04-30" "2000-05-31"
[6] "2000-06-30" "2000-07-31" "2000-08-31" "2000-09-30" "2000-10-31"
[11] "2000-11-30" "2000-12-31" "2001-01-31" "2001-02-28" "2001-03-31"
[16] "2001-04-30" "2001-05-31" "2001-06-30" "2001-07-31" "2001-08-31"
[21] "2001-09-30" "2001-10-31" "2001-11-30" "2001-12-31"
as.POSIXct(x) [1] "2000-01-01 GMT" "2000-02-01 GMT" "2000-03-01 GMT" "2000-04-01 GMT"
[5] "2000-05-01 GMT" "2000-06-01 GMT" "2000-07-01 GMT" "2000-08-01 GMT"
[9] "2000-09-01 GMT" "2000-10-01 GMT" "2000-11-01 GMT" "2000-12-01 GMT"
[13] "2001-01-01 GMT" "2001-02-01 GMT" "2001-03-01 GMT" "2001-04-01 GMT"
[17] "2001-05-01 GMT" "2001-06-01 GMT" "2001-07-01 GMT" "2001-08-01 GMT"
[21] "2001-09-01 GMT" "2001-10-01 GMT" "2001-11-01 GMT" "2001-12-01 GMT"
# 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)[1] "2005-02-01" "2005-03-03" "2005-04-02" "2005-05-01"
# 3rd Friday in last month of the quarter of Date x
## first day of last month of quarter
y <- as.Date(zoo::as.yearmon(zoo::as.yearqtr(x), frac = 1))
## number of days to first Friday
n <- sapply(y, function(z) which(format(z + 0:6, "%w") == "5")) - 1
## add number of days to third Friday
y + n + 14 [1] "2000-03-17" "2000-03-17" "2000-03-17" "2000-06-16" "2000-06-16"
[6] "2000-06-16" "2000-09-15" "2000-09-15" "2000-09-15" "2000-12-15"
[11] "2000-12-15" "2000-12-15" "2001-03-16" "2001-03-16" "2001-03-16"
[16] "2001-06-15" "2001-06-15" "2001-06-15" "2001-09-21" "2001-09-21"
[21] "2001-09-21" "2001-12-21" "2001-12-21" "2001-12-21"
suppressWarnings(RNGversion("3.5.0"))
set.seed(1)
z <- zoo(rnorm(24), x, frequency = 12)
z Jan 2000 Feb 2000 Mar 2000 Apr 2000 May 2000 Jun 2000
-0.62645381 0.18364332 -0.83562861 1.59528080 0.32950777 -0.82046838
Jul 2000 Aug 2000 Sep 2000 Oct 2000 Nov 2000 Dec 2000
0.48742905 0.73832471 0.57578135 -0.30538839 1.51178117 0.38984324
Jan 2001 Feb 2001 Mar 2001 Apr 2001 May 2001 Jun 2001
-0.62124058 -2.21469989 1.12493092 -0.04493361 -0.01619026 0.94383621
Jul 2001 Aug 2001 Sep 2001 Oct 2001 Nov 2001 Dec 2001
0.82122120 0.59390132 0.91897737 0.78213630 0.07456498 -1.98935170
as.ts(z) Jan Feb Mar Apr May Jun
2000 -0.62645381 0.18364332 -0.83562861 1.59528080 0.32950777 -0.82046838
2001 -0.62124058 -2.21469989 1.12493092 -0.04493361 -0.01619026 0.94383621
Jul Aug Sep Oct Nov Dec
2000 0.48742905 0.73832471 0.57578135 -0.30538839 1.51178117 0.38984324
2001 0.82122120 0.59390132 0.91897737 0.78213630 0.07456498 -1.98935170
## 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) 123 222
May 1999 4 NA
Jun 1999 NA NA
Jul 1999 NA NA
Aug 1999 NA NA
Sep 1999 NA NA
Oct 1999 NA NA
Nov 1999 NA NA
Dec 1999 NA NA
Jan 2000 NA NA
Feb 2000 NA 6
xx <- zoo(seq_along(x), x)
## aggregating over year
as.year <- function(x) as.numeric(floor(as.yearmon(x)))
aggregate(xx, as.year, mean)2000 2001
6.5 18.5