[R] constructing nxn matrices involving calculations& conditions based on other tables

Jabba jabbba at gmail.com
Mon May 30 16:20:49 CEST 2011


Hi Luisa,

it was really difficult to manage to understand what you need. Assuming
that i've done it correctly this should be similar to what you want:

A.matrix <- matrix(rpois(n=28,lambda=2),nrow=7)

M.matrix <- matrix(0,nrow=dim(A.matrix)[2],ncol=dim(A.matrix)[2])
for(i in 1:dim(df)[2])
  for(j in i:dim(df)[2]) {
  prod <- df[,i]*df[,j]
  M.matrix[i,j] <- length(which(prod!=0))
  }

sums <- apply(A.matrix,2,sum)
N.matrix <- matrix(0,nrow=dim(A.matrix)[2],ncol=dim(A.matrix)[2])
> for (i in 1:length(sums))
  for (j in i:length(sums)){
  diff <- sums[i]-sums[j]
  N.matrix[i,j] <- diff
  }

I briefly searched a way to to this without cycling inside objects, but
i wasn't able to do it straightforwardly. Probably somebody could
give a more efficient and simple hint.

Marco.



More information about the R-help mailing list