pairwise: Apply function to columns/elements pairwise Literally copied...

View source: R/utility.R

pairwiseR Documentation

Apply function to columns/elements pairwise Literally copied from the package AkselA/R-ymse: Ymse!!!

Description

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

Usage

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

Arguments

x

a matrix or data.frame

FUN

any function that takes two vectors as input and returns 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

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

https://rdrr.io/github/AkselA/R-ymse/src/R/pairwise.R


Fuschi/MetaGenomicR documentation built on Sept. 5, 2022, 5:23 a.m.