pairwise: Apply function to columns/elements pairwise

Description Usage Arguments Value See Also Examples

View source: R/pairwise.R

Description

Pairwise application of a function to the columns of a matrix/data.frame or elements of a list

Usage

1
pairwise(x, FUN, ..., comm = FALSE)

Arguments

x

a matrix or data.frame

FUN

any function that takes two vectors as input and returs a single value

...

further arguments passed to FUN

comm

logical; is FUN commutative? If true, only the lower triangle, including the diagonal, is computed

Value

An n×n square matrix with n the number of columns of x.

See Also

similarity for a few more examples

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
dtf <- data.frame(aa=c(1, 1, 2, 2, 3, 2, 4), 
                  bb=c(1, 1, 2, 3, 3, 3, 4),
                  cc=c(3, 3, 2, 1, 1, 1, 1),
                  dd=c(1, 2, 2, 2, 1, 1, 2))

# Root Mean Square Deviation
pairwise(dtf, function(x, y) sqrt(mean((x-y)^2)))

# using with cor.test() to accompany cor()                  
pv <- pairwise(dtf, function(x, y) cor.test(x, y)$p.val)
pvn <- 6^(1.1-pv)-5
pvn[pvn<1] <- 1

set_mar(1, 1, 1, 1)
plot(0, xlim=c(0.5, 4.5), ylim=c(0.5, 4.5), cex=0, ann=FALSE, xaxt="n", yaxt="n")
text(rep(1:4, 4), rep(4:1, each=4), t(round(cor(dtf), 2)), cex=pvn, 
  col=c("black", "darkgrey")[(pv>0.1)+1])

AkselA/R-ymse documentation built on March 21, 2020, 9:52 a.m.