pairwise: Pairwise operations

Description Usage Arguments Details Examples

View source: R/pairwise.R

Description

Apply a function to all possible pairwise combinations of rows or columns.

Usage

1
pairwise(x, margin = 2, fun)

Arguments

x

A matrix class object or data.frame that can be coerced to a numeric matrix.

margin

Work on rows (1) or columns (2)

fun

The function to apply. Must be applicable to numeric data.

Details

The function must work on all rows (columns) provided. While a data.frame is accepted, it will be coerced to a matrix, and so its rows (columns) must be atomic vectors that are not character strings. While it might be expanded to be more flexible at some point, it should be thought of as a way to create things like distance or covariance matrices with arbitrary functions. At some point a parallel version will be added.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
library(lazerhawk)
# dot product
pairwise(mtcars, 1,  function(x,y) sum(x*y))

# correlation
pairwise(mtcars, 2,  cor)

# correlation for factors
mydata = data.frame(a = factor(sample(letters[1:3], 10, replace=TRUE)),
                    b = factor(sample(letters[1:4], 10, replace=TRUE)))
pairwise(mydata, 2,  cor)

mclark--/lazerhawk documentation built on Sept. 8, 2020, 7:21 p.m.