Learn R Programming

oce (version 0.9-19)

binMean2D: 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)

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.

Value

A list with the following elements: the midpoints (renamed as x and y), the count (number) of f(x,y) values for x and y values that lie between corresponding breaks, and the resultant average (f) of f(x,y), classified by the x and y breaks.

See Also

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

Examples

Run this code
library(oce)
x <- runif(500)
y <- runif(500)
f <- x + y
xb <- seq(0, 1, 0.1)
yb <- seq(0, 1, 0.2)
m <- binMean2D(x, y, f, xb, yb)
plot(x, y)
contour(m$xmids, m$ymids, m$result, add=TRUE, levels=seq(0, 2, 0.5), labcex=1)

Run the code above in your browser using DataLab