[R] howto optimize operations between pairs of rows in a single matrix like cor and pairs
Adaikalavan Ramasamy
a.ramasamy at imperial.ac.uk
Mon Aug 25 00:35:27 CEST 2008
Hi,
I calculating the output of a function when applied to pairs of row from
a single matrix or dataframe similar to how cor() and pairs() work. This
is the code that I have been using:
pairwise.apply <- function(x, FUN, ...){
n <- nrow(x)
r <- rownames(x)
output <- matrix(NA, nc=n, nr=n, dimnames=list(r, r))
for(i in 1:n){
for(j in 1:n){
if(i >= j) next()
output[i, j] <- FUN( x[i,], x[j,] )
}
}
return(output)
}
I realize that the output of the pairwise operation needs to be scalar.
Here is an example. The actual function and dataset I want to use is
more complicated and thus the function runs slow for large datasets.
m <- iris[ 1:5, 1:4 ]
pairwise.apply(m, sum)
1 2 3 4 5
1 NA 19.7 19.6 19.6 20.4
2 NA NA 18.9 18.9 19.7
3 NA NA NA 18.8 19.6
4 NA NA NA NA 19.6
5 NA NA NA NA NA
Can I use apply() or any of it's family to optimize the codes? I have
tried playing around with outer, kronecker, mapply without any sucess.
Any suggestions? Thank you.
Regards, Adai
More information about the R-help
mailing list