binMean2D: Bin-average f=f(x,y)

View source: R/bin.R

binMean2DR Documentation

Bin-average f=f(x,y)

Description

Average the values of a vector f(x,y) in bins defined on vectors x and y. A common example might be averaging spatial data into location bins.

Usage

binMean2D(
  x,
  y,
  f,
  xbreaks,
  ybreaks,
  flatten = FALSE,
  fill = FALSE,
  fillgap = -1,
  include.lowest = FALSE,
  na.rm = FALSE
)

Arguments

x

vector of numerical values.

y

vector of numerical values.

f

Matrix of numerical values, a matrix f=f(x,y).

xbreaks

Vector of values of x at the boundaries between bins, calculated using pretty(x) if not supplied.

ybreaks

Vector of values of y at the boundaries between bins, calculated using pretty(y) if not supplied.

flatten

A logical value indicating whether the return value also contains equilength vectors x, y, z and n, a flattened representation of xmids, ymids, result and number.

fill

Logical value indicating whether to fill NA-value gaps in the matrix. Gaps will be filled as the average of linear interpolations across rows and columns. See fillgap, which works together with this.

fillgap

Integer controlling the size of gap that can be filled across. If this is negative (as in the default), gaps will be filled regardless of their size. If it is positive, then gaps exceeding this number of indices will not be filled.

include.lowest

logical value indicating whether to include y values for which the corresponding x is equal to xmin. See “Details”.

na.rm

logical value indicating whether to remove NA values before doing the computation of the average. This is passed to mean(), which does the work of the present function.

Value

By default, i.e. with flatten being FALSE, binMean2D() returns a list with the following elements: xmids, a vector holding the x-bin midpoints; ymids, a vector holding the y-bin midpoints; number, a matrix holding the number the points in each bin; and result, a matrix holding the mean value in each bin. If flatten is TRUE, the number and result matrices are renamed as n and f and transformed to vectors, while the bin midpoints are renamed as x and y and extended to match the length of n and f.

Author(s)

Dan Kelley

See Also

Other bin-related functions: binApply1D(), binApply2D(), binAverage(), binCount1D(), binCount2D(), binMean1D()

Examples

library(oce)
x <- runif(500, 0, 0.5)
y <- runif(500, 0, 0.5)
f <- x^2 + y^2
xb <- seq(0, 0.5, 0.1)
yb <- seq(0, 0.5, 0.1)
m <- binMean2D(x, y, f, xb, yb)
cm <- colormap(f, col=oceColorsTurbo)
opar <- par(no.readonly=TRUE)
drawPalette(colormap=cm)
plot(x, y, col=cm$zcol, pch=20, cex=1.4)
contour(m$xmids, m$ymids, m$result, add=TRUE, labcex=1.4)
par(opar)


oce documentation built on July 9, 2023, 5:18 p.m.