Learn R Programming

mosaic (version 1.9.1)

aggregatingFunction2: 2-ary aggregating functions

Description

aggregatinFuntion2 creates statistical summaries of two numerical vectors that are formula aware.

Usage

aggregatingFunction2(fun)

Value

a function that generalizes fun to handle a formula/data frame interface.

Arguments

fun

a function that takes two numeric vectors and computes a summary statistic, returning a numeric vector of length 1.

Details

This was designed to support functions like cov() which can be used to compute numerical summaries from two numeric vectors. The logic of the resulting function is this: 1) If the first two arguments are both formulas, then those formulas are evaluated (with data) to compute the covariance; (2) If the first argument is a formula, and the second is NULL, then the left and ride sides of the formula and data are used to create the vectors passed to fun; (3) Else everything is simply passed to fun for evaluation.

Examples

Run this code
if(require(mosaicData)) {
  foo <- aggregatingFunction2(stats::cor)
  foo(length ~ width, data = KidsFeet)
  stats::cor(KidsFeet$length, KidsFeet$width)
}

Run the code above in your browser using DataLab