Calculate a sum, product, minimum, or maximum for each non-overlapping period specified by INDEX.

period.sum(x, INDEX)

period.prod(x, INDEX)

period.max(x, INDEX)

period.min(x, INDEX)

Arguments

x

A univariate data object.

INDEX

A numeric vector of endpoints for each period.

Value

An xts or zoo object containing the sum, product, minimum, or maximum for each endpoint in INDEX.

Details

These functions are similar to calling period.apply() with the same endpoints and function. There may be slight differences in the results due to numerical accuracy.

For xts-coercible objects, an appropriate INDEX can be created by a call to endpoints().

Author

Jeffrey A. Ryan

Examples


x <- c(1, 1, 4, 2, 2, 6, 7, 8, -1, 20)
i <- c(0, 3, 5, 8, 10)

period.sum(x, i)
#>  3  5  8 10 
#>  6  4 21 19 
period.prod(x, i)
#>   3   5   8  10 
#>   4   4 336 -20 
period.min(x, i)
#>  3  5  8 10 
#>  1  2  6 -1 
period.max(x, i)
#>  3  5  8 10 
#>  4  2  8 20 

data(sample_matrix)
y <- sample_matrix[, 1]
ep <- endpoints(sample_matrix)

period.sum(y, ep)
#>       30       58       89      119      150      180 
#> 1506.342 1421.960 1535.487 1488.806 1497.902 1424.315 
period.sum(as.xts(y), ep)
#>                [,1]
#> 2007-01-31 1506.342
#> 2007-02-28 1421.960
#> 2007-03-31 1535.487
#> 2007-04-30 1488.806
#> 2007-05-31 1497.902
#> 2007-06-30 1424.315

period.prod(y, ep)
#>           30           58           89          119          150          180 
#> 1.056649e+51 5.757944e+47 3.468070e+52 7.433317e+50 1.608181e+52 1.969971e+50 
period.prod(as.xts(y), ep)
#>                    [,1]
#> 2007-01-31 1.056649e+51
#> 2007-02-28 5.757944e+47
#> 2007-03-31 3.468070e+52
#> 2007-04-30 7.433317e+50
#> 2007-05-31 1.608181e+52
#> 2007-06-30 1.969971e+50

period.min(y, ep)
#>       30       58       89      119      150      180 
#> 49.85477 50.22448 48.25248 48.90488 47.56210 47.19411 
period.min(as.xts(y), ep)
#>                [,1]
#> 2007-01-31 49.85477
#> 2007-02-28 50.22448
#> 2007-03-31 48.25248
#> 2007-04-30 48.90488
#> 2007-05-31 47.56210
#> 2007-06-30 47.19411

period.max(y, ep)
#>       30       58       89      119      150      180 
#> 50.74150 51.29502 50.81620 50.32009 49.59963 47.74899 
period.max(as.xts(y), ep)
#>                [,1]
#> 2007-01-31 50.74150
#> 2007-02-28 51.29502
#> 2007-03-31 50.81620
#> 2007-04-30 50.32009
#> 2007-05-31 49.59963
#> 2007-06-30 47.74899