Skip to content

Why sum and mean works different in apply.monthly? #124

Closed

Description

@fcasarramona

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.

Here is an example:

library(xts)
xts.ts <- xts(matrix(rnorm(462), nrow = 231),as.Date(13514:13744,origin="1970-01-01"))
dim(xts.ts)
[1] 231   2
dim(apply.monthly(xts.ts, mean))
[1] 8 2
dim(apply.monthly(xts.ts, sum))
[1] 8 1

The same is true with other xts apply functions.

This seems inconsistent.

Activity

  1. braverock commented on Oct 26, 2015

    @braverock
    Contributor

    Why not use colMeans and colSums to make sure you get what you think you're getting?

    x<-xts(matrix(rnorm(1000),ncol=10),order.by=as.Date(1:100))
    mean(x)
    sum(x)
    apply.weekly(x,sum)
    apply.weekly(x,mean)
    apply.weekly(x,colSums)
    apply.weekly(x,colMeans)
  2. fcasarramona commented on Oct 26, 2015

    @fcasarramona
    Author

    Thanks, just discovered I can do it with colSums.
    In this case, apply.weekly(x,sum) and apply.weekly(x,mean) should return 1 column xts both.

  3. joshuaulrich commented on Oct 26, 2015

    @joshuaulrich
    Owner

    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.

  4. fcasarramona commented on Nov 17, 2015

    @fcasarramona
    Author

    In my opinion is better just remove them and let colSums and his family functions do the work. This will be consistent with matrix.

  5. sushant-choudhary commented on Jul 11, 2017

    @sushant-choudhary

    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.

  6. pverspeelt commented on Jan 25, 2020

    @pverspeelt
    Contributor

    @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)

  7. joshuaulrich commented on Aug 2, 2023

    @joshuaulrich
    Owner

    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).

  8. added this to the 0.13.2 milestone on Aug 2, 2023
  9. added a commit that references this issue on Feb 23, 2024
    c4452e8
  10. added a commit that references this issue on May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions