R: Apply a function to matrix data

binApply2D {oce} | R Documentation

## 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, ...)

### 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 bins; calculated using `pretty` if not supplied.
`ybreaks` | values of y at the boundaries between bins; calculated using `pretty` if not supplied.
`FUN` | function to apply to the data
`...` | arguments to pass to the function `FUN`

### Value

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

### Author(s)

Dan Kelley

### See Also

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

### Examples

    library(oce)
    ## Not run:
    ## secchi depths in lat and lon bins
    if (require(ocedata)) {
        data(secchi, package="ocedata")
        col <- rev(oce.colorsJet(100))[rescale(secchi$depth,
                                               xlow=0, xhigh=20,
                                               rlow=1, rhigh=100)]
        zlim <- c(0, 20)
        breaksPalette <- seq(min(zlim), max(zlim), 1)
        colPalette <- rev(oce.colorsJet(length(breaksPalette)-1))
        drawPalette(zlim, "Secchi Depth", breaksPalette, colPalette)
        data(coastlineWorld)
        mapPlot(coastlineWorld, longitudelim=c(-5,20), latitudelim=c(50,66),
          grid=5, fill='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=zlim, col=colPalette)
        mapPolygon(coastlineWorld, col='gray')
    }

    ## End(Not run)

[Package oce version 0.9-20 Index]