Skip to content
Prev 360902 / 398506 Next

building a spatial matrix

Sorry, you're right.

The result line should be:

result.m[cbind(factor(result$fcell), factor(result$cellneigh))]  <-
result$distance


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[cbind(factor(result$fcell), factor(result$cellneigh))]  <-
result$distance

It's just about instantaneous on the dataset you sent me:


system.time({
result <- subset(censDist, f_cell %in% id_cell$f_cell & f_cell_neigh %in%
id_cell$f_cell)
result.m <- matrix(NA, nrow=nrow(id_cell), ncol=nrow(id_cell))
result.m[cbind(factor(result$f_cell), factor(result$f_cell_neigh))] <-
result$distance
})

  user  system elapsed
  0.361   0.007   0.368




Sarah

On Fri, May 13, 2016 at 10:36 AM, A M Lavezzi <mario.lavezzi at unipa.it>
wrote:
columns
of
#
result$distance
in
wrote:
columns
description of
result$id_neigh==j]
i
and
distance