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
{{ message }}
Repository navigation
Why sum and mean works different in apply.monthly? #124
When apply.monthly in a xts multivaluate time series (with more than one column) apply.monthly(xts.ts, sum) returns a one column time series but apply.monthly(xts.ts, mean) returns the same number of columns of the original.
The problem is that xts defines mean.xts in its namespace and it is being dispatched, even though it is not registered as a S3 method.
mean.xts was added when R Core decided that mean should not operate by-column for matrices and data.frames. Seems like it might be a good idea to rename both mean.xts and sd.xts as mean_xts and sd_xts, respectively, in order to avoid this inconsistency.
Or maybe they should just be removed all together? They're not exported, so CRAN likely would not allow their use in other packages.
Using colSums takes care of the sum function not being applied by columns, however what would be a similar fix for the sd( ) function, which, like sum( ), is not applied by columns in period.apply.
I just pushed a commit with the best solution I could think of. The current behavior is maintained, but prints a message every time period.apply(x, ep, FUN = mean) is called, unless options(xts.message.period.apply.mean = FALSE). This is a message instead of a warning because a warning would cause CRAN packages to fail R CMD check.
The message says to use FUN = colMeans for the current behavior and FUN = function(x) mean(x) to calculate the mean for all the data. Neither of those will print a message. I also added a note about this to the help files for period.apply() and apply.daily() (and friends).
Activity
braverock commentedon Oct 26, 2015
Why not use colMeans and colSums to make sure you get what you think you're getting?
fcasarramona commentedon Oct 26, 2015
Thanks, just discovered I can do it with
colSums.In this case,
apply.weekly(x,sum)andapply.weekly(x,mean)should return 1 columnxtsboth.joshuaulrich commentedon Oct 26, 2015
The problem is that xts defines
mean.xtsin its namespace and it is being dispatched, even though it is not registered as a S3 method.mean.xtswas added when R Core decided thatmeanshould not operate by-column for matrices and data.frames. Seems like it might be a good idea to rename bothmean.xtsandsd.xtsasmean_xtsandsd_xts, respectively, in order to avoid this inconsistency.Or maybe they should just be removed all together? They're not exported, so CRAN likely would not allow their use in other packages.
fcasarramona commentedon Nov 17, 2015
In my opinion is better just remove them and let
colSumsand his family functions do the work. This will be consistent withmatrix.sushant-choudhary commentedon Jul 11, 2017
Using colSums takes care of the sum function not being applied by columns, however what would be a similar fix for the sd( ) function, which, like sum( ), is not applied by columns in period.apply.
pverspeelt commentedon Jan 25, 2020
@sushant-choudhary ,
A late answer, but the package matrixStats has all these functions available for columns and rows. Using the example above that would be:
apply.weekly(x, matrixStats::colSds)joshuaulrich commentedon Aug 2, 2023
I just pushed a commit with the best solution I could think of. The current behavior is maintained, but prints a message every time
period.apply(x, ep, FUN = mean)is called, unlessoptions(xts.message.period.apply.mean = FALSE). This is a message instead of a warning because a warning would cause CRAN packages to failR CMD check.The message says to use
FUN = colMeansfor the current behavior andFUN = function(x) mean(x)to calculate the mean for all the data. Neither of those will print a message. I also added a note about this to the help files forperiod.apply()andapply.daily()(and friends).Document when period.apply() is called with `mean`
(math/R-xts) Updated 0.13.1 to 0.14.1
(math/R-xts) Updated 0.13.1 to 0.14.1
(math/R-xts) Updated 0.13.1 to 0.14.1
(math/R-xts) Updated 0.13.1 to 0.14.1