Learn R Programming

oce (version 1.7-10)

binApply2D: Apply a function to matrix data

Description

The function FUN is applied to f in bins specified by xbreaks and ybreaks. (If FUN is mean(), consider using binMean2D() instead, since it should be faster.)

Usage

binApply2D(x, y, f, xbreaks, ybreaks, FUN, ...)

Value

A list with the following elements: the breaks in x and y (i.e. xbreaks and ybreaks), the break mid-points (i.e. xmids and ymids), and a matrix containing the result of applying FUN() to the f values, as subsetted by these breaks.

Arguments

x

a vector of numerical values.

y

a vector of numerical values.

f

a vector of data to which the elements of FUN may be supplied

xbreaks

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

ybreaks

as xbreaks, but for y.

FUN

univariate function that is applied to the f data within any given bin

...

arguments to pass to the function FUN

Author

Dan Kelley

See Also

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

Examples

Run this code
library(oce)
# \donttest{
## secchi depths in lat and lon bins
if (requireNamespace("ocedata", quietly=TRUE)) {
    data(secchi, package="ocedata")
    ## Note that zlim is provided to the colormap(), to prevent a few
    ## points from setting a very wide scale.
    cm <- colormap(z=secchi$depth, col=oceColorsViridis, zlim=c(0, 15))
    par(mar=c(2, 2, 2, 2))
    drawPalette(colormap=cm, zlab="Secchi Depth")
    data(coastlineWorld)
    mapPlot(coastlineWorld, longitudelim=c(-5, 20), latitudelim=c(50, 66),
      grid=5, col='gray', projection="+proj=lcc +lat_1=50 +lat_2=65")
    bc <- binApply2D(secchi$longitude, secchi$latitude,
                     pretty(secchi$longitude, 80),
                     pretty(secchi$latitude, 40),
                     f=secchi$depth, FUN=mean)
    mapImage(bc$xmids, bc$ymids, bc$result, zlim=cm$zlim, col=cm$zcol)
    mapPolygon(coastlineWorld, col="gray")
}
# }

Run the code above in your browser using DataLab