Skip to content

Commit

Permalink
Remove unnecessary as.matrix() call
Browse files Browse the repository at this point in the history
The period.XXX() functions (sum, prod, min, max) only work on
univariate series, so the as.matrix() call is not necessary. It makes
the functions considerably slower, because it converts the xts index
into row names for the matrix.

See #278.
  • Loading branch information
joshuaulrich committed Dec 20, 2018
1 parent 138d80c commit 6547ffb
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions R/period.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function(x,INDEX) {
if(ep[1L] != 0L) ep <- c(0L,ep)
if(ep[length(ep)] != NROW(x)) ep <- c(ep,NROW(x))

xx <- as.double(as.matrix(x))
xx <- as.double(x)
xa <- .Call("xts_period_sum", xx, ep, PACKAGE = "xts")

if(timeBased(index(x))) {
Expand All @@ -61,7 +61,7 @@ function(x,INDEX) {
if(ep[1] != 0L) ep <- c(0L,ep)
if(ep[length(ep)] != NROW(x)) ep <- c(ep,NROW(x))

xx <- as.double(as.matrix(x))
xx <- as.double(x)
xa <- .Call("xts_period_prod", xx, ep, PACKAGE = "xts")

if(timeBased(index(x))) {
Expand All @@ -79,7 +79,7 @@ function(x,INDEX) {
if(ep[1] != 0L) ep <- c(0L,ep)
if(ep[length(ep)] != NROW(x)) ep <- c(ep,NROW(x))

xx <- as.double(as.matrix(x))
xx <- as.double(x)
xa <- .Call("xts_period_max", xx, ep, PACKAGE = "xts")

if(timeBased(index(x))) {
Expand All @@ -97,7 +97,7 @@ function(x,INDEX) {
if(ep[1] != 0L) ep <- c(0L,ep)
if(ep[length(ep)] != NROW(x)) ep <- c(ep,NROW(x))

xx <- as.double(as.matrix(x))
xx <- as.double(x)
xa <- .Call("xts_period_min", xx, ep, PACKAGE = "xts")

if(timeBased(index(x))) {
Expand Down

0 comments on commit 6547ffb

Please sign in to comment.