# [R] building a spatial matrix

Sarah Goslee sarah.goslee at gmail.com
Thu May 12 15:51:13 CEST 2016

```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

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:
> Hello,
>
> I have a sample of 1327  locations, each one idetified by an id and a
> numerical code.
>
> I need to build a spatial matrix, say, M, i.e. a 1327x1327 matrix
> collecting distances among the locations.
>
> M(i,i) should be 0, M(i,j) should contain the distance among location i and
> j
>
> I shoud use data organized in the following way:
>
> 1) id_cell contains the identifier (id) of each location (1...1327) and the
> numerical code of the location (f_cell) (see head of id_cell below)
>
>      id  f_cell
> 1    1   2120
> 12  2     204
> 22  3   2546
> 24  4   1327
> 34  5   1729
> 43  6   2293
>
> 2) censDist contains, for each location identified by its numerical code,
> the distance to other locations (censDist has 1.5 million rows). The
>
> location 2924 has a distance to 2732 of 1309.7525
> location 2924 has a distance to 2875 of 696.2891,
> etc.
>
>   f_cell f  _cell_neigh  distance
> 1   2924         2732   1309.7525
> 2   2924         2875     696.2891
> 3   2924         2351   1346.0561
> 4   2924         2350   1296.9804
> 5   2924         2725   1278.1877
> 6   2924         2721   1346.9126
>
>
> Basically, for every location in  id_cell I should pick up the distance to
> other locations in id_cell from censDist, and allocate it in M
>
> I have not come up with a satisfactory vectorizion of this problem and
> using a loop is out of question.
>