stats-na.omit: Handle missing values in 'timeSeries' objects

naR Documentation

Handle missing values in 'timeSeries' objects


Functions for handling missing values in "timeSeries" objects.


## S4 method for signature 'timeSeries'
na.omit(object, method = c("r", "s", "z", "ir", "iz", "ie"), 
    interp = c("before", "linear", "after"), FUN, ...)

## Deprecated:
removeNA(x, ...)
substituteNA(x, type = c("zeros", "mean", "median"), ...)
interpNA(x, method = c("linear", "before", "after"), ...)



an object of class "timeSeries".


for na.omit, the method of handling NAs; for interpNA, how to interpolate the matrix column by column, see Section ‘Details’.

interp, type

Three alternative methods are provided to remove NAs from the data: type="zeros" replaces the missing values with zeros, type="mean" replaces the missing values with the column mean, type="median" replaces the missing values with the column median.


a function or a name of a function, such as "mean" or median. FUN is applied to the non-NA values in each column to determine the replacement value. The call looks like FUN(coli, na.rm = TRUE), so FUN should have argument na.rm. All arguments except object are ignored if FUN is specified.


a numeric matrix, or any other object which can be transformed into a matrix through x = as.matrix(x, ...). If x is a vector, it will be transformed into a one-dimensional matrix.


arguments to be passed to the function as.matrix.


Functions for handling missing values in "timeSeries" objects and in objects which can be transformed into a vector or a two dimensional matrix.

For na.omit argument method specifies the method how to handle NAs. Can be one of the following strings:


na.rm = FALSE, skip, i.e. do nothing,


remove NAs,


substitute NAs by zeros,


interpolate NAs and remove NAs at the beginning and end of the series,


interpolate NAs and substitute NAs at the beginning and end of the series,


interpolate NAs and extrapolate NAs at the beginning and end of theseries.

For interpNA argument method specifies how to interpolate the matrix column by column. One of the following character strings: "linear", "before", "after". For interpolation the function approx is used.

The functions are listed by topic.

na.omit Handles NAs,
removeNA Removes NAs from a matrix object,
substituteNA substitute NAs by zero, the column mean or median,
interpNA interpolates NAs using R's "approx" function.

Missing Values in Price and Index Series:

Applied to timeSeries objects the function removeNA just removes rows with NAs from the series. For an interpolation of time series points one can use the function interpNA. Three different methods of interpolation are offered: "linear" does a linear interpolation, "before" uses the previous value, and "after" uses the following value. Note, that the interpolation is done on the index scale and not on the time scale.

Missing Values in Return Series:

For return series the function substituteNA may be useful. The function allows to fill missing values either by method="zeros", the method="mean" or the method="median" value of the appropriate columns.


When dealing with daily data sets, there exists another function alignDailySeries which can handle missing data in un-aligned calendrical "timeSeries" objects.

The functions removeNA, substituteNA and interpNA are older implementations. Please use in all cases if possible the new function na.omit.

Additional remarks by GNB:

removeNA(x) is equivalent to na.omit(x) or na.omit(x), methods = "r".

interpNA can be replaced by a call to na.omit with argument method equal to ir, iz, or ie, and argument "interp" equal to the "method" argument for interpNA (note that the defaults are not the same).

substituteNA(x, type = "zeros") is equivalent to na.omit(x, method = "z"). For other values of type one can use argument FUN, as in na.omit(x, FUN = "mean").

A final remark: the three deprecated functions are non-generic. removeNA(x) is completely redundant as it simply calls na.omit. The other two however may be useful for matrix-like objects. Please inform the maintainer of the package if you use them on objects other than from class "timeSeries" and wish them kept in the future.


Troyanskaya O., Cantor M., Sherlock G., Brown P., Hastie T., Tibshirani R., Botstein D., Altman R.B., (2001); Missing Value Estimation Methods for DNA microarrays Bioinformatics 17, 520–525.

See Also



X <- matrix(rnorm(100), ncol = 5)  # Create a Matrix X
X[3, 5] <- NA                      # Replace a Single NA Inside
X[17, 2:4] <- c(NA, NA, NA)        # Replace Three in a Row Inside
X[13:15, 4] <- c(NA, NA, NA)       # Replace Three in a Column Inside
X[11:12, 5] <- c(NA, NA)           # Replace Two at the Right Border
X[20, 1] <- NA                     # Replace One in the Lower Left Corner
Xts <- timeSeries(X)  # convert X to timeSeries Xts

## Remove Rows with NAs

## Subsitute NA's with zeros or column means (formerly substituteNA())
na.omit(Xts, method = "z")
na.omit(Xts, FUN = "mean")
na.omit(Xts, FUN = "median")

## Subsitute NA's with a trimmed mean
na.omit(Xts, FUN = function(x, na.rm) mean(x, trim = 0.10, na.rm = na.rm))

## Interpolate NA's Linearily (formerly interpNA())
na.omit(X, method = "ir", interp = "linear")
na.omit(X, method = "iz", interp = "linear")
na.omit(X, method = "ie", interp = "linear")
## Take Previous Values in a Column
na.omit(X, method = "ir", interp = "before")
na.omit(X, method = "iz", interp = "before")
na.omit(X, method = "ie", interp = "before")

## examples with X (which is a matrix, not "timeSeries"
## (these examples are not run automatically as these functions are
## deprecated.) 
## Remove Rows with NAs - 
## Subsitute NA's by Zeros or Column Means - 
   substituteNA(X, type = "zeros")
   substituteNA(X, type = "mean")
## Interpolate NA's Linearily - 
   interpNA(X, method = "linear")
   # Note the corner missing value cannot be interpolated!
## Take Previous Values in a Column - 
   interpNA(X, method = "before")
   # Also here, the corner value is excluded

timeSeries documentation built on Aug. 26, 2023, 5:08 p.m.