Learn R Programming

oce (version 0.9-18)

binApply: Apply a function to binned matrix data

Description

Apply a function to binned matrix data

Usage

binApply1D(x, f, xbreaks, FUN, ...)
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.

Details

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

Examples

Run this code
library(oce)
## (a) 1D: salinity profile with median and quartile 1 and 3
data(ctd)
p <- ctd[["pressure"]]
S <- ctd[["salinity"]]
q1 <- binApply1D(p, S, pretty(p, 30), function(x) quantile(x, 1/4))
q3 <- binApply1D(p, S, pretty(p, 30), function(x) quantile(x, 3/4))
plotProfile(ctd, "salinity", col='gray', type='n')
polygon(c(q1$result, rev(q3$result)),
c(q1$xmids, rev(q1$xmids)), col='gray')
points(S, p, pch=20)

## This requires the rgdal package to be installed
## (b) 2D: 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')
}

Run the code above in your browser using DataLab