Hi,
Try:
?indx<- combn(ncol(uu),2)
sapply(seq_len(ncol(indx)),function(i) rho(uu[,indx[1,i]],uu[,indx[2,i]]))
# [1] 0.9746318 0.9594119 0.9512583 0.9462555 0.9981909 0.9961499 0.9946367
# [8] 0.9996186 0.9990560 0.9998746
A.K.
On Sunday, January 26, 2014 10:04 AM, Kathryn Lord <kathryn.lord2000 at
gmail.com> wrote:
Dear R users,
I'd like to compute "rho"(looks like a correlation matrix) with
every two
columns of "uu" matrix below.
Toy example,
> uu <- matrix(1:15, nr=3, nc=5)
> uu
? ? [,1] [,2] [,3] [,4] [,5]
[1,]? ? 1? ? 4? ? 7? 10? 13
[2,]? ? 2? ? 5? ? 8? 11? 14
[3,]? ? 3? ? 6? ? 9? 12? 15
rho <- function(x,y)
{
? ? sum(x*y)/(sqrt(sum(x^2))*sqrt(sum(y^2)))
}
rho_12 <- rho(uu[,1],uu[,2])
rho_13 <- rho(uu[,1],uu[,3])
rho_14 <- rho(uu[,1],uu[,4])
rho_15 <- rho(uu[,1],uu[,5])
rho_23 <- rho(uu[,2],uu[,3])
rho_24 <- rho(uu[,2],uu[,4])
rho_24 <- rho(uu[,2],uu[,5])
rho_34 <- rho(uu[,3],uu[,4])
rho_35 <- rho(uu[,3],uu[,5])
rho_45 <- rho(uu[,4],uu[,5])
Actually, the matrix uu is huge, 20*1000.? Would you plz tell me how to
calculate "rho", more efficiently??
Any suggestion will be greatly appreciated.
Best,
Kathryn Lord
??? [[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.