我希望按周对每帧中每列的值求和。我可以做到这一点,但总和由于某种原因不起作用:
> zoo.data <- zoo(data.frame(x=11:20,y=1:10),as.Date(1:10,origin="1970-01-01"))
> apply.weekly(zoo.data, mean)
x y
1970-01-04 12 2
1970-01-11 17 7
> apply.weekly(zoo.data, sum)
1970-01-04 1970-01-11
42 168
发生了什么事?
答案 0 :(得分:8)
这是因为 xts 作者决定添加mean.xts
方法来模仿基本R的旧行为(实际上是colMeans
)。现在,mean.xts
对象而不是xts
分发mean.default
,apply.weekly
暂时将您的zoo
对象内部转换为xts
。
R> apply.weekly(zoo.data, mean)
x y
1970-01-04 12 2
1970-01-11 17 7
R> apply.weekly(zoo.data, mean.default)
1970-01-04 1970-01-11
7 12
但是,我认为这就是你想要做的事情:
R> apply.weekly(zoo.data, colMeans)
x y
1970-01-04 12 2
1970-01-11 17 7
R> apply.weekly(zoo.data, colSums)
x y
1970-01-04 36 6
1970-01-11 119 49