Billy Pilgrim
Billy Pilgrim

Reputation: 31

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

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:

Upvotes: 1

Views: 495

Answers (1)

Nick Kennedy
Nick Kennedy

Reputation: 12640

This should work:

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

Upvotes: 1

Related Questions