[R] aggregate in zoo
Gabor Grothendieck
ggrothendieck at gmail.com
Fri Jun 1 13:23:38 CEST 2007
On 6/1/07, Alfonso Sammassimo <cincinattikid at bigpond.com> wrote:
> Hi R-experts,
>
> Thanks very much to Jim Holtman and Gabor on my previous question.
>
> I am having another problem with data manipulation in zoo. The following is
> data (Z) for first business day of every month in zoo format. I am trying to
> get mean of "open" for each year. I subset Z <- Z[,2] then
>
> > sapply(split(Z, format(index(Z), "%Y")),mean)
>
> I get error message:
>
> >2000 2001 2002 2003 2004 2005 2006 2007
> NA NA NA NA NA NA NA NA
> Warning messages:
> 1: argument is not numeric or logical: returning NA in: mean.default(X[[1]],
> ...)
> 2: argument is not numeric or logical: returning NA in: mean.default(X[[2]],
> ...)
> etc...................
>
> Any help on what I'm missing would be appreciated. I am particularly
> confused by the fact that the command used works fine on the original data
> file (i.e. before subsetting by first day of month). Sorry if I have
> overlooked something very simple.
>
> <Z
> dayofmonth open
> 2000-02-01 01 1636.10
> 2000-03-01 01 1596.75
> 2000-04-03 03 1737.70
> 2000-05-01 01 1695.65
> 2000-06-01 01 1651.90
> 2000-07-03 03 1669.20
> 2000-08-01 01 1628.35
> 2000-09-01 01 1717.35
> 2000-10-02 02 1614.55
> 2000-11-01 01 1587.10
> 2000-12-01 01 1475.60
> 2001-01-02 02 1450.65
> 2001-02-01 01 1503.60
> 2001-03-01 01 1351.95
> 2001-04-02 02 1268.10
> 2001-05-01 01 1369.20
> 2001-06-01 01 1362.75
> 2001-07-02 02 1331.55
> 2001-08-01 01 1309.70
> 2001-09-04 04 1235.55
> 2001-10-01 01 1109.20
> 2001-11-01 01 1155.55
> 2001-12-03 03 1207.30
Can't tell what your Z really looks like,
try posting dput(Z) or explain how to create
Z from scratch, but at any rate your code has two
problems:
1. the result is not a zoo object (that may or may not be a problem)
2. your are combining the two columns altogether
and then taking the mean of that
Try copying and pasting this into your
session:
Lines <- "date dayofmonth open
2000-02-01 01 1636.10
2000-03-01 01 1596.75
2000-04-03 03 1737.70
2000-05-01 01 1695.65
2000-06-01 01 1651.90
2000-07-03 03 1669.20
2000-08-01 01 1628.35
2000-09-01 01 1717.35
2000-10-02 02 1614.55
2000-11-01 01 1587.10
2000-12-01 01 1475.60
2001-01-02 02 1450.65
2001-02-01 01 1503.60
2001-03-01 01 1351.95
2001-04-02 02 1268.10
2001-05-01 01 1369.20
2001-06-01 01 1362.75
2001-07-02 02 1331.55
2001-08-01 01 1309.70
2001-09-04 04 1235.55
2001-10-01 01 1109.20
2001-11-01 01 1155.55
2001-12-03 03 1207.30
"
library(zoo)
z <- read.zoo(textConnection(Lines), header = TRUE)
year <- function(x) as.numeric(format(x, "%Y"))
sapply(split(z[,2], year(index(z))), mean)
# last line could be replaced with just this
aggregate(z[,2], year, mean)
More information about the R-help
mailing list