0

Here is my data in "xts" "zoo" format:

                   OIL      SP   YEN   GOLD SILVER COPPER
2015-02-11 08:00:00 49.88 2059.00 83.51 1235.9 16.920 255.75
2015-02-11 08:05:00 49.78 2058.25 83.51 1235.9 16.930 255.65
2015-02-11 08:10:00 49.85 2058.75 83.51 1235.9 16.945 255.65
2015-02-11 08:15:00 49.74 2059.00 83.53 1235.9 16.925 255.15
2015-02-11 08:20:00 49.64 2059.50 83.48 1235.9 16.925 255.40
2015-02-18 08:00:00 52.53 2094.00 83.82 1208.5 16.415 258.85
2015-02-18 08:05:00 52.38 2094.75 83.82 1208.5 16.445 259.55
2015-02-18 08:10:00 52.35 2094.00 83.83 1208.5 16.460 260.40
2015-02-18 08:15:00 52.35 2094.25 83.84 1208.5 16.480 260.90
2015-02-18 08:20:00 52.40 2093.25 83.85 1208.5 16.480 260.45

I would like to calculate the mean of each column for the two separate days listed as the index. The mean on 2015-02-11 is ... and the mean on 2015-02-18 is....

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pete
  • 15
  • 4

3 Answers3

0

You need to extract the day from the timestamp and use that to drive a call to tapply (tapply documentation). See also this comparison of the various apply functions.

Community
  • 1
  • 1
Lutz Prechelt
  • 36,608
  • 11
  • 63
  • 88
0

You could try aggregate

 res <- aggregate(.~as.Date(index(xt1)), xt1, FUN=mean)
 colnames(res)[1] <- 'Date'
 res
 #      Date    OIL      SP    YEN   GOLD SILVER COPPER
 #1 2015-02-11 49.778 2058.90 83.508 1235.9 16.929 255.52
 #2 2015-02-18 52.402 2094.05 83.832 1208.5 16.456 260.03

Update

For extracting the coef of linear regression by group ("date")

 lapply(split(xt1, as.Date(index(xt1))), 
              function(x) coef(lm(OIL~SP, data=x)))

data

 xt1 <- structure(c(49.88, 49.78, 49.85, 49.74, 49.64, 52.53, 52.38, 
 52.35, 52.35, 52.4, 2059, 2058.25, 2058.75, 2059, 2059.5, 2094, 
 2094.75, 2094, 2094.25, 2093.25, 83.51, 83.51, 83.51, 83.53, 
 83.48, 83.82, 83.82, 83.83, 83.84, 83.85, 1235.9, 1235.9, 1235.9, 
 1235.9, 1235.9, 1208.5, 1208.5, 1208.5, 1208.5, 1208.5, 16.92, 
 16.93, 16.945, 16.925, 16.925, 16.415, 16.445, 16.46, 16.48, 
 16.48, 255.75, 255.65, 255.65, 255.15, 255.4, 258.85, 259.55, 
 260.4, 260.9, 260.45), .Dim = c(10L, 6L), .Dimnames = list(NULL, 
 c("OIL", "SP", "YEN", "GOLD", "SILVER", "COPPER")), 
index =  structure(c(1423659600, 
1423659900, 1423660200, 1423660500, 1423660800, 1424264400, 
 1424264700, 
 1424265000, 1424265300, 1424265600), tzone = "",
 tclass = c("POSIXct", "POSIXt")), class = c("xts", "zoo"), 
.indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"), 
.indexTZ = "", tzone = "")
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Is it possible to use this method to calculate a linear regression on oil ~ sp and extract the coefficient by date? – Pete Feb 27 '15 at 18:10
0

Use aggregate.zoo like this:

aggregate(xt1, as.Date, mean)

giving the following zoo object:

              OIL      SP    YEN   GOLD SILVER COPPER
2015-02-11 49.778 2058.90 83.508 1235.9 16.929 255.52
2015-02-18 52.402 2094.05 83.832 1208.5 16.456 260.03

See ?aggregate.zoo for more info.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341