将函数应用于单独的日期
apply function to separate dates
这是我的 "xts" "zoo" 格式的数据:
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
我想计算列为索引的两天中每列的平均值。 2015-02-11 的平均值是...,2015-02-18 的平均值是....
您需要从时间戳中提取日期并使用它来驱动对 tapply
的调用(tapply documentation). See also this comparison of the various apply functions。
你可以试试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
更新
用于按组提取线性回归的coef
("date")
lapply(split(xt1, as.Date(index(xt1))),
function(x) coef(lm(OIL~SP, data=x)))
数据
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 = "")
像这样使用aggregate.zoo
:
aggregate(xt1, as.Date, mean)
给出以下动物园对象:
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
有关详细信息,请参阅 ?aggregate.zoo
。
这是我的 "xts" "zoo" 格式的数据:
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
我想计算列为索引的两天中每列的平均值。 2015-02-11 的平均值是...,2015-02-18 的平均值是....
您需要从时间戳中提取日期并使用它来驱动对 tapply
的调用(tapply documentation). See also this comparison of the various apply functions。
你可以试试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
更新
用于按组提取线性回归的coef
("date")
lapply(split(xt1, as.Date(index(xt1))),
function(x) coef(lm(OIL~SP, data=x)))
数据
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 = "")
像这样使用aggregate.zoo
:
aggregate(xt1, as.Date, mean)
给出以下动物园对象:
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
有关详细信息,请参阅 ?aggregate.zoo
。