Closed stulacy closed 1 year ago
it really depends on the function being called. the contract for how input is handled and what the output means is really defined by the function.. eg: ATR
takes multiple columns in and returns multiple columns. I'd be careful that modifying period.apply
to work the way suggested could break almost as many things as its trying to fix.
I agree with @ethanbsmith. period.apply
would have to be a lot more complex to figure out what the supplied function returned in order to work column-wise for any combination of function and xts data input.
mean
works column-wise because the default method (stats::mean
) used to. Then it was changed to work only for vectors in order to be consistent with other functions (e.g. median
). That broke some packages that depend on xts on relied on the original behavior of stats::mean
. I wish I would not have made the change in xts because of issues like this.
That's fair enough, I hadn't realised that's how stats::mean
used to work. Would it be useful to add a sentence to the docs explaining that custom functions won't apply column-wise by default?
I'm closing this because it won't (currently) result in any code changes. It's also related to #124.
Description
When running
period.apply
to calculate time-based averages onxts
objects with more than 1 column, a single aggregate over all columns is returned for each time-period rather than a column-wise aggregation, unless the mean is used. The reason for this is there is amean.xts
method that determines whether the input is multi-dimesional and returns column-wise means if so. However, other aggregation functions (median, max, min etc...) do not have methods forxts
and thus run the standard base function.A solution would be to check if the data is multi-dimensional within
period.apply
and calculate multi-dimensional aggregates if so, rather than delegating this behaviour to specific methods.I can submit a PR if this would be helpful.
Expected behavior
period.apply
returns column-wise aggregates for multi-dimensional time-series regardless of which aggregation function is used.Minimal, reproducible example
Session Info