0

I am trying to run a spatial auto correlogram for a project looking at deforestation in the Atlantic forest, Brazil.

I am however confused as to why I am hitting this problem.

Problem

When I run the initial part of my code i receive an error of

Error: ncol(x) == 2 is not TRUE

My code is

r.nb <- dnearneigh(as.matrix(shapeS$POINT_X,shapeS$POINT_Y),
                   d1=200, d2=100000, latlong=FALSE)

and then I hope to move run this code

p.cor <- sp.correlogram(r.nb, deforestation, order=15,
                        method="I", randomisation=FALSE)

r.nb <- dnearneigh(as.matrix(shapeS$POINT_X,shapeS$POINT_Y), 
                   d1=200, d2=100000, latlong=FALSE)

My data is

A vector data set with the headings

POINTID GRID_CODE POINT_X POINT_Y
rafa.pereira
  • 13,251
  • 6
  • 71
  • 109
Guy Benett
  • 23
  • 8

1 Answers1

0

You need to use cbind, not as.matrix, or the approach that I show below. Always identify the R packages you are using. You claim that your data set a 'vector data set'. I doubt that. I am assuming it is a matrix.

If it is a matrix, you can do

m <- shapeS[, c('POINT_X', 'POINT_Y')]
library(spdep)
r.nb <- dnearneigh(m, d1=200, d2=100000, latlong=FALSE)

It it is a data.frame, you can do

m <- as.matrix(shapeS[, c('POINT_X', 'POINT_Y')])

or

m <- cbind(shapeS$POINT_X, shapeS$POINT_Y)
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63