na_approx: Replace NA by Interpolation

View source: R/data-na-approx.R

na_approxR Documentation

Replace NA by Interpolation

Description

Replace NA by Interpolation

Usage

na_approx(x, ...)

## Default S3 method:
na_approx(
  x,
  fun = function(z) {
     zoo::na.approx(z, na.rm = FALSE)
 },
  min.length = 3L,
  ...
)

## S3 method for class 'data.frame'
na_approx(x, ...)

## S3 method for class 'matrix'
na_approx(x, ...)

Arguments

x

an object.

...

further arguments passed to methods.

fun

Interpolation zoo::na.approx(z) zoo::na.fill(z, "extend") or

min.length

min length

Value

data.frame

Examples


dat <- data.frame(
  u = c(NA, 2,  3,  4,  6,  NA, 7, NA),
  v = c(NA, NA, NA, NA, 6,  4,  7, 2),
  w = c(NA, 2,  NA, 4,  6,  4,  7, 2),
  x = c(NA, 2,  3,  7,  6,  4,  7, NA),
  y = c(NA, 7,  2,  NA, 1,  9,  3, 4),
  z = c( 1, 7,  2,  8,  NA, 9,  3, 4)
)

na_approx(dat$z)
na_approx(dat)
na_approx(as.matrix(dat))


dat <- data.frame(
time = c(0, 2, 6, 12, 24, 48, 3 * 24),
temperature = c(20, 24, 26, NA, 29, 30, 30.2)
)

rst <- with(dat,
            approx(time,
                   temperature,
                   xout = time[is.na(temperature)]))
dat$temperature[which(dat$time ==   rst$x)] <-  rst$y


with(dat, plot(temperature~time))
dat


stp4/stp25tools documentation built on Feb. 27, 2025, 11:14 p.m.