sgbp length and number of elements do not agree

I created a large sgbp object with sf::intersects. Trying to use it fails badly because when I unlist it, I get an array that is smaller than the length.

length(intersections)
[1] 1629542
length(unlist(intersections))
[1] 1629249

I'm completely mystified as to how this could happen, and so have no idea how to make a reasonable reprex. Any suggestions are welcome.

unlist() drops empty elements from intersections

library(sf)
#> Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE

a <- st_sfc(
  st_point(c(0,0)),
  st_point(c(1,1)),
  st_point(c(100,100))  
)

b <- st_sfc(
  st_buffer(st_point(c(0,0)), 1),
  st_buffer(st_point(c(1,1)), 1)
)

intersections <- st_intersects(a, b)

intersections
#> Sparse geometry binary predicate list of length 3, where the predicate
#> was `intersects'
#>  1: 1
#>  2: 2
#>  3: (empty)

length(intersections)
#> [1] 3

length(unlist(intersections))
#> [1] 2

Created on 2025-12-09 with reprex v2.1.1

1 Like

Yes, by process of elimination I found that intersects had created some values of "integer(0)", so dropping those points from the original file fixed it.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.