# [R] pairwise difference operator

Gabor Grothendieck ggrothendieck at myway.com
Mon Aug 2 19:10:54 CEST 2004

```Adaikalavan Ramasamy <ramasamy <at> cancer.org.uk> writes:

:
: Thank you to Marc Schwartz and Gabor Grothendieck for their responses.
: Both solutions are useful.
:
: It would be nice to generalise this problem to situations where other
: operations besides difference. Maybe a new member of apply family -
: pwapply for pairwise apply ?
:
: Of course the output would be different if the results of an operation
: on two columns produce a vector (like pairwise difference here) or a
: single value (like in correlation or pairwise t-test) and one need to
: somehow account for this.

Since the general case would involve columns or array slices between
two not necessarily identical arrays I think the general case is really
just a sort of generalized inner product.

In the case that the generalized difference is a scalar its usually called
a product and the Euclidean inner product is the most common and takes
the form of matrix multiplication which can be done in the one of
the following ways:

res1 <- t(mat) %*% mat
res2 <- crossprod(mat, mat)
res3 <- crossprod(mat)

A generalized inner product, f, replacing the Euclidean one can be
obtained using a double apply like this:

res4 <- apply(mat, 2, function(a) apply(mat, 2, function(b) sum(a*b)))

where sum(a*b) can be replaced by f(a,b) for a general inner product
function.  This actually works even if f returns a vector or other
array; however, you may need to reshape the result in that case.

In  the above cases the two matrices were the same but, as mentioned,
they need not be and you can't count on symmetry between f(a,b) and
f(b,a).   If your two matrices are the saame and  you can count on
symmetry then you may want only the lower triangular part and in
that case you can use lower.tri like this:

res4[lower.tri(res4)]

```