camr_linear_interpolation: Function for Linear Interpolation

View source: R/R05-Statistical_tools.R

camr_linear_interpolationR Documentation

Function for Linear Interpolation

Description

Given a pair of x and y values, uses linear interpolation to compute a new x or y value.

Usage

camr_linear_interpolation(vec, interp_y = TRUE)

Arguments

vec

A vector of 5 values, consisting of...

x0

The x coordinate for the lower boundary;

y0

The y coordinate for the lower boundary;

x1

The x coordinate for the upper boundary;

y1

The y coordinate for the upper boundary;

x or y

The coordinate at which interpolation should occur.

A named vector can be provided, otherwise the order is assumed to follow the list given above.

interp_y

Logical; if TRUE, the value to interpolate is assumed to be a y coordinate.

Value

The interpolated x or y value. If outside the lower or upper boundaries, NA is returned instead.

Author(s)

Kevin Potter

Examples

# Linear interpolation for y
inp <- c( x0 = 0, y0 = 0, x1 = 1, y1 = 2, x = .5 )
camr_linear_interpolation( inp )
# Linear interpolation for x
inp <- c( x0 = 0, y0 = 0, x1 = 1, y1 = 2, y = .5 )
camr_linear_interpolation( inp, FALSE )
# Linear interpolation across multiple values
x = c( 0, 1, 2 )
y = c( 0, 2, 4 )
# Create matrix with 5th column for points to interpolate at
m = cbind( x[-3], y[-3], x[-1], y[-1], c( .5, 1.5 ) )
# Linear interpolation for y values
apply( m, 1, camr_linear_interpolation )
# Linear interpolation for x values (NA for value outside boundary)
apply( m, 1, camr_linear_interpolation, interp_y = FALSE )


rettopnivek/camrprojects documentation built on Dec. 20, 2024, 10:17 p.m.