period.apply: Apply Function Over Specified Interval

Description Usage Arguments Details Value Author(s) See Also Examples

Description

Apply a specified function to data over a given interval, where the interval is taken to be the data from INDEX[k] to INDEX[k+1], for k=1:(length(INDEX)-1).

Usage

1
period.apply(x, INDEX, FUN, ...)

Arguments

x

data to apply FUN to

INDEX

numeric vector specifying indexing

FUN

an argument of type function

...

additional arguments for FUN

Details

Similar to the rest of the apply family, calculate a specified functions value given a shifting set of data values. The primary difference is that it is that period.apply applies a function to non-overlapping intervals along a vector.

Useful for applying arbitrary functions over an entire data object by an aribirtary index, as when INDEX is the result of a call to breakpoints.

Value

A vector with length of INDEX minus 1

Author(s)

Jeffrey A. Ryan

See Also

endpoints apply.monthly

Examples

1
2
3
4
5
6
7
zoo.data <- zoo(rnorm(31)+10,as.Date(13514:13744,origin="1970-01-01"))
ep <- endpoints(zoo.data,'weeks')
period.apply(zoo.data, INDEX=ep, FUN=function(x) mean(x))
period.apply(zoo.data, INDEX=ep, FUN=mean)                  #same

glue <- function(x) { paste(x,collapse='') }
period.apply(letters,c(0,5,7,26),glue)

Example output

Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

2007-01-07 2007-01-14 2007-01-21 2007-01-28 2007-02-04 2007-02-11 2007-02-18 
  9.985477  10.171038  10.166399  10.213543   9.614604  10.358458  10.114449 
2007-02-25 2007-03-04 2007-03-11 2007-03-18 2007-03-25 2007-04-01 2007-04-08 
 10.606461   9.632002   9.977275  10.236541  10.207991  10.215559   9.746748 
2007-04-15 2007-04-22 2007-04-29 2007-05-06 2007-05-13 2007-05-20 2007-05-27 
 10.438813   9.931465  10.290737   9.730534  10.111358  10.073892  10.588534 
2007-06-03 2007-06-10 2007-06-17 2007-06-24 2007-07-01 2007-07-08 2007-07-15 
  9.872487   9.899542  10.337888  10.087279  10.187546   9.589103  10.371288 
2007-07-22 2007-07-29 2007-08-05 2007-08-12 2007-08-19 
 10.072825  10.561280   9.793787   9.985477  10.171038 
2007-01-07 2007-01-14 2007-01-21 2007-01-28 2007-02-04 2007-02-11 2007-02-18 
  9.985477  10.171038  10.166399  10.213543   9.614604  10.358458  10.114449 
2007-02-25 2007-03-04 2007-03-11 2007-03-18 2007-03-25 2007-04-01 2007-04-08 
 10.606461   9.632002   9.977275  10.236541  10.207991  10.215559   9.746748 
2007-04-15 2007-04-22 2007-04-29 2007-05-06 2007-05-13 2007-05-20 2007-05-27 
 10.438813   9.931465  10.290737   9.730534  10.111358  10.073892  10.588534 
2007-06-03 2007-06-10 2007-06-17 2007-06-24 2007-07-01 2007-07-08 2007-07-15 
  9.872487   9.899542  10.337888  10.087279  10.187546   9.589103  10.371288 
2007-07-22 2007-07-29 2007-08-05 2007-08-12 2007-08-19 
 10.072825  10.561280   9.793787   9.985477  10.171038 
     [,1]                 
[1,] "abcde"              
[2,] "fg"                 
[3,] "hijklmnopqrstuvwxyz"

xts documentation built on May 2, 2019, 5:18 p.m.