xts icon indicating copy to clipboard operation
xts copied to clipboard

period.apply inconsistent behaviour with mean and median functions

Open wrobell opened this issue 9 years ago • 1 comments

The issue below is for xts 0.9.

Please consider the following example

library(xts)

data = data.frame(a=1:5, b=11:15, ts=as.POSIXct(1:5, origin='2015-10-10'))
data = xts(data[,c('a', 'b')], order.by=data$ts)

ends = endpoints(data, 'seconds', 2)

Taking mean of values every 2s gives data frame with a and b columns

> period.apply(data, ends, mean)
                      a    b
2015-10-10 01:00:01 1.0 11.0
2015-10-10 01:00:03 2.5 12.5
2015-10-10 01:00:05 4.5 14.5

I would expect period.apply to behave in similar way for median function, but

> period.apply(data, ends, median)
                    [,1]
2015-10-10 01:00:01  6.0
2015-10-10 01:00:03  7.5
2015-10-10 01:00:05  9.5

Also

> period.apply(data, ends, function(x) mean(x))
                    [,1]
2015-10-10 01:00:01  6.0
2015-10-10 01:00:03  7.5
2015-10-10 01:00:05  9.5

but according to the documentation above should be the same as period.apply(data, ends, mean).

wrobell avatar Dec 02 '15 13:12 wrobell

Thanks for the reproducible example. This is related to #124. If mean.xts is either removed or renamed to mean_xts, then period.apply(data, ends, mean) returns a 1-column object consistent with your other examples.

It is interesting that period.apply(data, ends, mean) and period.apply(data, ends, function(x) mean(x)) don't return the same results. Given the direction decided in #124, I'm not going to investigate this curiosity further.

joshuaulrich avatar Dec 05 '15 17:12 joshuaulrich

Closing as duplicate of #124.

joshuaulrich avatar Oct 10 '22 19:10 joshuaulrich