interpolate: Interpolate values

Description Usage Arguments Value Examples

View source: R/interpolate.R

Description

Interpolate values

Usage

1
interpolate(x, y, xout, method = "linear", extrapolate = TRUE)

Arguments

x

coordinate where data is avalaible.

y

vector of values measured at points x.

xout

vector giving the coordinates at which to interpolate.

method

interpolation method. When y is numeric (or coercible as such), the method can be "constant", "linear", or "spline". When y is discrete (factor, character, etc.), "constant" (i.e. nearest neighbour) interpolation is always performed, no mater what is specified.

extrapolate

when TRUE, the default, extrapolate y for values of xout outside of range(x).

Value

The vector of interpolated values at points xout.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
set.seed(1)

# interpolate numerical values
x <- jitter(1:10, 2)
y <- runif(10, 10, 100)
xout <- seq(0,11,by=0.1)
plot(x, y, type="b", xlim=c(0,11))
rug(x)
rug(xout, col="red")
points(xout, interpolate(x, y, xout, method="constant"), col="red")
points(xout, interpolate(x, y, xout, method="linear"), col="darkgreen", pch=4)
points(xout, interpolate(x, y, xout, method="spline", extrapolate=FALSE), col="blue", pch=3)

# interpolate a factor
x <- jitter(1:10, 2)
y <- factor(letters[1:2][round(runif(10, 1, 2))])
xout <- 1:10
plot(x, y, yaxt="n", ylim=c(0,3))
axis(2, at=1:2, labels=levels(y))
rug(x)
rug(xout, col="red")
points(xout, interpolate(x, y, xout, method="spline"), col="red")

jiho/castr documentation built on April 5, 2020, 2:12 p.m.