My goal is to find neighbors from my SpatialPolygons object using the poly2nb() command from the sp package in R.

This is my code thus far:

   p <- s_gis %>%
   select(long, lat) %>%
   ps <- Polygons(list(p), 1000)
   sps <- SpatialPolygons(list(ps))

where s_gis is my dataset. I'm otherwise able to make a successful SpatialPolygons object except for the fact that the slot for plotOrder reads 1, instead of 636353 like it is in my dataset.

Running the poly2nb() commands returns an error Error in sp$mbxv[i]:(n * 2) : argument of length 0 which I'm assuming is coming from the fact that it's finding 0 neighbors given that I only have a plotOrder of 1.

How do I modify the plotOrder?

In addition, regarding my error message from the poly2nb() command, if the problem runs deeper, can someone shed some insight as to why?

I have also tried ps@plotOrder <- s_gis$order to no avail.


#get lat, long, and groupid
s_gis_latlon <- s_gis %>%
  select(long,lat, group)

#group lists by group id
p_groups <- split(s_gis_latlon, s_gis_latlon$group)

#delete group id so that we can have 2-column matrix for Polygon
p_groups1 <- lapply(p_groups, function(x) { x[,3] <- NULL; x })

# turn each element of list into a polygon
p_groups1 <- lapply(names(p_groups1),
                    function(x) {b <- Polygon(p_groups1[[x]])

#turn list of polygons into Polygons
ps <- Polygons(p_groups1,1)

data <- s_gis %>%
  group_by(group) %>%
  summarize(district_wealthrange = mean(wealthrange))

#create SpatialPolygons object
sps <- SpatialPolygons(ps@Polygons)

I'm getting the error Error in SpatialPolygons(ps@Polygons) : no slot of name "Polygons" for this object of class "Polygon"

  • What's s_gis? Can you show us its summary() and maybe its head()? What does sps end up as? Is it one polygon or many? How are you running poly2nb to get that error? What are you running it on? On sps? Do you care about the plot order or working out the neighbourhood? – Spacedman Apr 5 at 23:37
  • s_gis is one of the datasets I'm using. The summary is quite large; however, I did select out long and lat which results in a tibble with dimensions 661228 x 2. sps ends up Slot "plotOrder": [1] 1 Slot "labpt": [1] 76.83899 29.49845 Slot "ID": [1] "1000" Slot "area": [1] 196.6975 Slot "coords": # I removed these to save char. space Slot "plotOrder": [1] 1 Slot "bbox": min max x 68.186249 97.41529 y 6.755953 37.07827 Slot "proj4string": CRS arguments: +proj=longlat +datum=WGS84 – Lathan Apr 5 at 23:46
  • I'm runnning poly2nb(sps, queen = FALSE) and I care more about working out the neighborhood. Thanks so much for your quick response! – Lathan Apr 5 at 23:49

Now we know enough to reproduce your error message, I'll do that with a data frame of lat-long numbers:

> library(sp)
> s_gis = data.frame(long=runif(5), lat=runif(5))

that should be equivalent to your s_gis with lat-long selected. Continue with your code to make sps:

> p = Polygon(s_gis)
> ps = Polygons(list(p), 1000)
> sps = SpatialPolygons(list(ps))

and now use the spdep package:

> library(spdep)
Loading required package: Matrix
> poly2nb(sps, queen=FALSE)
Error in sp$mbxv[i]:(n * 2) : argument of length 0

and there's your error.

poly2nb computes the adjacency matrix between polygons. How many polygons have you got?

> length(sps)
[1] 1
> plot(sps)

enter image description here

One! (although it looks a bit like two but that's because the random lines cross over. This is one ring). No wonder it failed! This data is simply a set of point coordinates in each row - how do you think this data frame could define more than one polygon?

  • I've edited my original question. I've tried grouping my coordinates by a group id (which corresponds to each polygon). However, I'm getting a strange lists within a list issue when I make my SpatialPolygon object. The correct plot order is in my sps object; however, it's nested within a list which I can't seem to index. Thus, poly2nb() is still returning the same error message since it's not seeing the plot order and consequently not seeing the correct list of multiple polygon objects that I have. – Lathan Apr 6 at 18:24
  • You've completely changed the question now. I answered your question about the error you were getting from poly2nb (which didn't even relate to the title about the plot order, but never mind) and now you've added a load of stuff that gives a different error, you've still not shown us some sample data to make a reproducible answer, and I suspect you've left out some important packages necessary to make your code run. I would revert your edits, mark my answer as an answer, and create a new question with your new code, some sample data, and your new error message. – Spacedman Apr 7 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.