na.locf.xts: Last Observation Carried Forward

Description Usage Arguments Details Value Author(s) References Examples

View source: R/na.R

Description

xts method replace ‘NA’ with most recent non-‘NA’

Usage

1
2
## S3 method for class 'xts'
na.locf(object, na.rm = FALSE, fromLast = FALSE, maxgap=Inf, ...)

Arguments

object

an xts object

na.rm

logical. Should leading/trailing ‘NA’'s be removed? The default for xts FALSE is different than the default S3 method in the zoo package.

fromLast

logical. Cause observations to be carried backward rather than forward. Default is FALSE.

maxgap

runs of more than ‘maxgap’ will retain ‘NA’s after the maximum gap specified. See na.locf in the zoo package.

...

unused

Details

This is the xts method for the S3 generic na.locf. The primary difference to note is that after the ‘NA’ fill action is carried out, the default it to leave trailing or leading ‘NA’'s in place. This is different than zoo behavior.

Value

See the documentation in zoo.

Author(s)

Jeffrey A. Ryan

References

‘zoo’

Examples

1
2
3
4
5
6
7
x <- xts(1:10, Sys.Date()+1:10)
x[c(1,2,5,9,10)] <- NA

x
na.locf(x)
na.locf(x, fromLast=TRUE)
na.locf(x, na.rm=TRUE, fromLast=TRUE)

Example output

Loading required package: zoo

Attaching package: 'zoo'

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

    as.Date, as.Date.numeric

           [,1]
2017-07-22   NA
2017-07-23   NA
2017-07-24    3
2017-07-25    4
2017-07-26   NA
2017-07-27    6
2017-07-28    7
2017-07-29    8
2017-07-30   NA
2017-07-31   NA
           [,1]
2017-07-22   NA
2017-07-23   NA
2017-07-24    3
2017-07-25    4
2017-07-26    4
2017-07-27    6
2017-07-28    7
2017-07-29    8
2017-07-30    8
2017-07-31    8
           [,1]
2017-07-22    3
2017-07-23    3
2017-07-24    3
2017-07-25    4
2017-07-26    6
2017-07-27    6
2017-07-28    7
2017-07-29    8
2017-07-30   NA
2017-07-31   NA
           [,1]
2017-07-22    3
2017-07-23    3
2017-07-24    3
2017-07-25    4
2017-07-26    6
2017-07-27    6
2017-07-28    7
2017-07-29    8

xts documentation built on Sept. 13, 2020, 5:09 p.m.