binMean {oce} | R Documentation

## Bin-count or bin-average f=f(x) or f=f(x,y), based on x or (x,y) values

### Description

Bin-count or bin-average f=f(x) or f=f(x,y), based on x or (x,y) values

### Usage

    binCount1D(x, xbreaks)
    binCount2D(x, y, xbreaks, ybreaks, flatten=FALSE)
    binMean1D(x, f, xbreaks)
    binMean2D(x, y, f, xbreaks, ybreaks, flatten=FALSE, fill=FALSE)

### Arguments

`x` | a vector of numerical values.
`y` | a vector of numerical values.
`f` | a vector of numerical values, f=f(x) for the 1D function and f=f(x,y) for the 2D function. If missing, the `value` component of the return value will consist entirely of `NA` values.
`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.
`flatten` | if `TRUE`, then the return value also contains equi-length 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.

### Details

The `f` vector is averaged in bins defined for `x`. Missing values in `f` are ignored.

### Value

A list with the following elements: the breaks (`xbreaks`, along with `ybreaks` for the 2D case), the break mid-points (`xmids` along with `ymids` for the 2D case), the number of data points in each bin, `number`, and (for the “mean” case) the mean value of `f` value in the bins, `value`. If `flatten` is `TRUE`, then the return value also contains `x` and `y` (mid-points) and `z` (values) in a flattened grid, with `n` being the number in each cell.

For the 1D case, `number` and `mean` are vectors, whereas they are matrices for the 2D case. For plotting, the midpoints are more useful than the breaks, as shown in the examples.

### Author(s)

Dan Kelley

### Examples

    library(oce)
    ## A. fake linear data
    x <- seq(0, 100, 1)
    f <- 1 + 2 * x
    plot(x, f, pch=1)
    m <- binMean1D(x, f)
    points(m$xmids, m$result, pch=3, col='red', cex=3)

    ## B. fake quadratic data
    f <- 1 + x ^2
    plot(x, f, pch=1)
    m <- binMean1D(x, f)
    points(m$xmids, m$result, pch=3, col='red', cex=3)

    ## C. natural data
    data(co2)
    plot(co2, col='gray')
    m <- binMean1D(time(co2), co2)
    lines(m$xmids, m$result, type='o')

    ## D. 2D
    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)
    grid()
    contour(m$xmids, m$ymids, m$result, add=TRUE, levels=seq(0, 2, 0.5), labcex=1)
    mf <- binMean2D(x, y, f, xb, yb, flatten=TRUE)
    text(mf$x, mf$y, round(mf$f, 1), col=2)

[Package oce version 0.9-18 Index]