Generic functions for replacing each NA with interpolated values.
Usage
na.approx(object, ...)
## S3 method for class 'zoo'
na.approx(object, x = index(object), xout, ..., na.rm = TRUE, maxgap = Inf, along)
## S3 method for class 'zooreg'
na.approx(object, ...)
## S3 method for class 'ts'
na.approx(object, ...)
## Default S3 method:
na.approx(object, x = index(object), xout, ..., na.rm = TRUE, maxgap = Inf, along)
na.spline(object, ...)
## S3 method for class 'zoo'
na.spline(object, x = index(object), xout, ..., na.rm = TRUE, maxgap = Inf, along)
## S3 method for class 'zooreg'
na.spline(object, ...)
## S3 method for class 'ts'
na.spline(object, ...)
## Default S3 method:
na.spline(object, x = index(object), xout, ..., na.rm = TRUE, maxgap = Inf, along)
Arguments
object
object in which NAs are to be replaced
x, xout
Variables to be used for interpolation as in approx.
na.rm
logical. If the result of the (spline) interpolation still results in leading and/or trailing NAs, should these be removed (using na.trim)?
maxgap
maximum number of consecutive NAs to fill. Any longer gaps will be left unchanged. Note that all methods listed above can accept maxgap as it is ultimately passed to the default method. In na.spline the maxgap argument cannot be combined with xout, though.
along
deprecated.
…
further arguments passed to methods. The n argument of approx is currently not supported.
Details
Missing values (NAs) are replaced by linear interpolation via approx or cubic spline interpolation via spline, respectively.
It can also be used for series disaggregation by specifying xout.
By default the index associated with object is used for interpolation. Note, that if this calls index.default this gives an equidistant spacing 1:NROW(object). If object is a matrix or data.frame, the interpolation is done separately for each column.
If obj is a plain vector then na.approx(obj, x, y, xout, …) returns approx(x = x[!na], y = coredata(obj)[!na], xout = xout, …) (where na indicates observations with NA) such that xout defaults to x. Note that if there are less than two non-NAs then approx() cannot be applied and thus no NAs can be replaced.
If obj is a zoo, zooreg or ts object its coredata value is processed as described and its time index is xout if specified and index(obj) otherwise. If obj is two dimensional then the above is applied to each column separately. For examples, see below.
If obj has more than one column, the above strategy is applied to each column.
Value
An object of similar structure as object with NAs replaced by interpolation. For na.approx only the internal NAs are replaced and leading or trailing NAs are omitted if na.rm = TRUE or not replaced if na.rm = FALSE.
## using na.approx for disaggregationzy<-zoo(1:3, 2000:2001)# yearly to monthly serieszmo<-na.approx(zy, xout =as.yearmon(2000+0:13/12))zmo
Jan 2000 Feb 2000 Mar 2000 Apr 2000 May 2000 Jun 2000 Jul 2000 Aug 2000
1.000000 1.083333 1.166667 1.250000 1.333333 1.416667 1.500000 1.583333
Sep 2000 Oct 2000 Nov 2000 Dec 2000 Jan 2001
1.666667 1.750000 1.833333 1.916667 2.000000
1 1 NA 1 NA
2 2 NA 2 NA
3 3 NA 3 NA
4 4 NA 4 NA
5 5 NA 5 NA
# using na.approx to create regularly spaced series# z has points at 10, 20 and 40 minutes while output also has a point at 30if(require("chron")){tt<-as.chron("2000-01-01 10:00:00")+c(1, 2, 4)*as.numeric(times("00:10:00"))z<-zoo(1:3, tt)tseq<-seq(start(z), end(z), by =times("00:10:00"))na.approx(z, xout =tseq)}