You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When running period.apply to calculate time-based averages on xts 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 a mean.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 for xts 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.
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?
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 a
mean.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
The text was updated successfully, but these errors were encountered: