Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

na.trim

Trim Leading/Trailing Missing Observations


Description

Generic function for removing leading and trailing NAs.

Usage

na.trim(object, ...)
## Default S3 method:
na.trim(object, sides = c("both", "left", "right"), 
	is.na = c("any", "all"), maxgap = Inf, ...)

Arguments

object

an object.

sides

character specifying whether NAs are to be removed from both sides, just from the left side or just from the right side.

is.na

If "any" then a row will be regarded as NA if it has any NAs. If "all" then a row will be regarded as NA only if all elements in the row are NA. For one dimensional zoo objects this argument has no effect.

maxgap

maximum number of consecutive NAs to trim. Any longer gaps will be left unchanged.

...

further arguments passed to methods.

Value

An object in which leading and/or trailing NAs have been removed.

See Also

Examples

# examples of na.trim
x <- zoo(c(1, 4, 6), c(2, 4, 6))
xx <- zoo(matrix(c(1, 4, 6, NA, 5, 7), 3), c(2, 4, 6))
na.trim(x)
na.trim(xx)

# using na.trim for alignment
# cal defines the legal dates
# all dates within the date range of x should be present
cal <- zoo(,c(1, 2, 3, 6, 7))
x <- zoo(c(12, 16), c(2, 6))
na.trim(merge(x, cal))

zoo

S3 Infrastructure for Regular and Irregular Time Series (Z's Ordered Observations)

v1.8-10
GPL-2 | GPL-3
Authors
Achim Zeileis [aut, cre] (<https://orcid.org/0000-0003-0918-3766>), Gabor Grothendieck [aut], Jeffrey A. Ryan [aut], Joshua M. Ulrich [ctb], Felix Andrews [ctb]
Initial release
2022-04-15

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.