Learn R Programming

timeSeries (version 4041.111)

na.contiguous: Find longest contiguous stretch of non-NAs or check for NAs

Description

Find the longest consecutive stretch of non-missing values in a "timeSeries" object. In the event of a tie, the first such stretch. Also, "timeSeries" method for is.na.

Usage

# S3 method for timeSeries
na.contiguous(object, ...)
# S4 method for timeSeries
is.na(x)

Value

for the na.contiguous method, a "timeSeries" object without missing values,

for the is.na method, a "timeSeries" object whose data part is a logical matrix of the same dimension as in x indicating if the corresponding values are NA or not.

Arguments

object,x

a "timeSeries" object.

...

further arguments passed to other methods.

Examples

Run this code
## Dummy 'timeSeries' containing NAs
set.seed(2023)
data <- matrix(sample(c(1:20, rep(NA,4))), ncol = 2)
s <- timeSeries(data, timeCalendar(2023))
is.na(s)  
## Find the longest consecutive non-missing values
na.contiguous(s)

## tied longest stretches: 1:3, 6:9 and 10:12
x <- c(1:3, NA, NA, 6:8, NA, 10:12)
## should return the 1st one
na.contiguous(x)             # correct for R > 4.3.0
na.contiguous(timeSeries(x)) # correct for timeSeries version > 4030.106 

Run the code above in your browser using DataLab