I'd set the geometry elements to st_empty()
using a logical subselection. Here's a reproducible example that only uses the sf
package - you seem to have some other unstated packages attached.
First lets make a sample data set:
d = st_as_sf(
data.frame(
district_x=c(1,2,3,4),district_y=c(1,5,3,2),
x=c(1,2,3,4),y=c(1,2,3,4)),
coords=c("x","y"))
Which is this:
> d
Simple feature collection with 4 features and 2 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 1 ymin: 1 xmax: 4 ymax: 4
CRS: NA
district_x district_y geometry
1 1 1 POINT (1 1)
2 2 5 POINT (2 2)
3 3 3 POINT (3 3)
4 4 2 POINT (4 4)
Which rows are we interested in? Let's compare columns:
> d$district_x == d$district_y
[1] TRUE FALSE TRUE FALSE
Change those elements:
d$geometry[d$district_x == d$district_y] = st_point()
And now they are changed:
> d
Simple feature collection with 4 features and 2 fields (with 2 geometries empty)
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 2 ymin: 2 xmax: 4 ymax: 4
CRS: NA
district_x district_y geometry
1 1 1 POINT EMPTY
2 2 5 POINT (2 2)
3 3 3 POINT EMPTY
4 4 2 POINT (4 4)
Actually I'm not sure if I've changed the ones you want or the ones you didn't, but flip the test if so. Like:
> # reset `d`:
> d = st_as_sf(data.frame(district_x=c(1,2,3,4),district_y=c(1,5,3,2),x=c(1,2,3,4),y=c(1,2,3,4)), coords=c("x","y"))
> # test and set
> d$geometry[d$district_x != d$district_y] = st_point()
> # look
> d
Simple feature collection with 4 features and 2 fields (with 2 geometries empty)
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 1 ymin: 1 xmax: 3 ymax: 3
CRS: NA
district_x district_y geometry
1 1 1 POINT (1 1)
2 2 5 POINT EMPTY
3 3 3 POINT (3 3)
4 4 2 POINT EMPTY
The principle is: Create a vector of TRUE/FALSE for each row, set the geometry for the TRUE elements.
Strictly you should probably use st_geometry
to get the geometry rather than $geometry
since it might not be named that way in another data set:
> st_geometry(d)[d$district_x != d$district_y] = st_point()
Note this doesn't need any other packages which seem to be confusing you. Using simple constructions that can be easily taken apart and inspected will advance your learning.
st_geometry(pts)[which(pts$district_x == pts$district_y)] <- NULL
The call towhich
is simply creating an index, of the resulting query, that references the corresponding row number.