[R] filling large matrix( or dist object) with values from small matrix.

Nevil Amos nevil.amos at gmail.com
Thu Nov 18 09:31:40 CET 2010


The script below provides a trivial example of filling a large matrix 
with values from a small one with corresponding row and column details.

this is fine for a small example but too slow for many large examples ( 
my small matrices are 65x65 and my large matrices up to 900x900, and I 
have many permutations to fill.

is there a more efficient way to achieve this

M<-as.matrix(as.dist(matrix(x,nrow=4,ncol=4,dimnames=list(c("A","B","C","D"),c("A","B","C","D")))))
N<-matrix(NA,nrow=8,ncol=8,dimnames=list(c("A","B","C","D","A","B","C","D"),c("A","B","C","D","A","B","C","D")))
for (r in 1:nrow(N)){
for (c in 1:(ncol(N))){
N[r,c]=M[rownames(N)[r],colnames(N)[c]]
}
}

thanks

Nevil Amos



More information about the R-help mailing list