xts icon indicating copy to clipboard operation
xts copied to clipboard

rowMeans / rowSums on xts objects

Open harvey131 opened this issue 6 years ago • 0 comments

Description

Are you willing to accept a pull request for functions to compute the rowMeans / rowSums on xts objects and return the object as an xts object?

Minimal, reproducible example

  library(xts) 
  x <- .xts(matrix(rnorm(30), ncol=3), 1:10, dimnames=list(NULL, c('a','b','c')) )
  rowSums.xts <- function(x, na.rm=F, name='sum')
  {
    r <- matrix(rowSums(x, na.rm=na.rm), dimnames = list(NULL, name))
    xcoredata(r) <- xcoredata(x)
    return(r)
  }
  y <- rowSums.xts(x)
  all.equal(as.numeric(y), rowSums(x))

> head(x)
                              a           b          c
1969-12-31 19:00:01  0.01232592  0.95329017  1.9034343
1969-12-31 19:00:02 -1.16103541  0.97207964  0.4450646
1969-12-31 19:00:03 -0.34155275 -1.02157653  0.9267735
1969-12-31 19:00:04  2.00349972  0.09655199  0.1963148
1969-12-31 19:00:05 -0.15090314  0.58546213  0.5210464
1969-12-31 19:00:06  1.31294536 -0.28664174 -0.5481664

> head(y)
                           sum
1969-12-31 19:00:01  2.8690504
1969-12-31 19:00:02  0.2561089
1969-12-31 19:00:03 -0.4363558
1969-12-31 19:00:04  2.2963665
1969-12-31 19:00:05  0.9556054
1969-12-31 19:00:06  0.4781372

harvey131 avatar Dec 14 '18 21:12 harvey131