Skip to content
Prev 360866 / 398506 Next

building a spatial matrix

I don't see any reason why a loop is out of the question, and
answering would have been much easier if you'd included the requested
reproducible data, but what about this?

This solution is robust to pairs from idcell being absent in censDist,
and to the difference from A to B being different than the distance
from B to A, but not to A-B appearing twice. If that's possible,
you'll need to figure out how to manage it.

# create some fake data

idcell <- data.frame(
  id = seq_len(5),
  fcell = sample(1:100, 5))

censDist <- expand.grid(fcell=seq_len(100), cellneigh=seq_len(100))
censDist$distance <- runif(nrow(censDist))

# assemble the non-symmetric distance matrix
result <- subset(censDist, fcell %in% idcell$fcell & cellneigh %in%
idcell$fcell)
result.m <- matrix(NA, nrow=nrow(idcell), ncol=nrow(idcell))
result.m[factor(result$fcell), factor(result$cellneigh)] <- result$distance

Sarah
On Thu, May 12, 2016 at 5:26 AM, A M Lavezzi <mario.lavezzi at unipa.it> wrote: