Clip Function – How to Use It in the SF Package in R

clipintersectrsf

I try to clip shape "a" with shape "b". The result should be a polygon with the attributes and borders of "a" but cliped on the extent of "b". Basically a simple clip as we know it from ArcMap or QField.
Now I want to use the sf package in R. But all the suggestions I find tell me to use““st_intersection“`.
But this is not actually a clip because it will result in a layer containing also the borders of "b".

a<-st_read("a.shp")
be<-st_read("b.shp"
Clip1<- st_intersection(a,b)

What I want

What I want

what st_intersection does

what st_intersect does

Best Answer

This all works for me:

> library(sf)
> plot(input$geom, col=input$colour)
> plot(clip$geom, col=NA, border="red", add=TRUE)

enter image description here

> output = st_intersection(input, clip)
> plot(output$geom, col=output$colour)

enter image description here

The output object has the attributes from the underlying input object:

> output 
Simple feature collection with 3 features and 1 field
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 350715.5 ymin: 556199.3 xmax: 378850.3 ymax: 579962.8
CRS:            27700
  colour                       geometry
1  green POLYGON ((365346.3 579873.5...
2   grey POLYGON ((377844 569519.7, ...
3   blue POLYGON ((354933 574695.8, ...
>