How to make a numeric matrix out of a non-numeric one (or workaround)

493 views Asked by At

When I try to calculate a distance matrix and a corresponding weights matrix with the spacom package in R, I encounter the following problem (find here a replicable example):

> distmatrix <- DistanceMatrix(mafialomb, "NOME_COM", longlat = FALSE)
> weights <- WeightMatrix(distmatrix, 5000000)
> is.numeric(distmatrix)
TRUE
> is.numeric(weights)
FALSE

In order to proceed with the estimation, I need the weights matrix to be numeric. R Studio tells me that the distmatrix is data, while weights is a "Formal class dsCMatrix" and falls under "Values". When I call:

> head(weights)

I get the following description:

6 x 8 sparse Matrix of class "dgCMatrix"

Do you know of:

  • any way to make this matrix numeric; or
  • any other method to calculate spatial weights that would output a numeric weights matrix?
1

There are 1 answers

0
Nick Kennedy On

This should work:

nWeights <- as.matrix(weights)
is.numeric(nWeights)
#TRUE