Until this gets changed you might want to define a left_join
method for sf
classes that does the conversion for you:
left_join.sf =
function(x,y,by=NULL,copy=FALSE,suffix=c(".x",".y"),...){
ret = NextMethod("left_join")
st_as_sf(ret)
}
Example: before:
> nc = st_read(system.file("shape/nc.shp", package="sf"))
> newdata = data.frame(CRESS_ID=1:100,Z=runif(100))
> nc2 = left_join(nc, newdata)
Joining, by = "CRESS_ID"
> class(nc2)
[1] "data.frame"
Then with left_join.sf
, just using left_join(...)
does:
> nc2 = left_join(nc, newdata)
Joining, by = "CRESS_ID"
> class(nc2)
[1] "sf" "data.frame"
If you look at the source for what left_join
does on a plain R data frame, you'll see it follows the same pattern - the actual join is done on a tbl
version of x
, and then its converted back to a data frame:
> dplyr:::left_join.data.frame
function (x, y, by = NULL, copy = FALSE, ...)
{
as.data.frame(left_join(tbl_df(x), y, by = by, copy = copy,
...))
}
You can cast an sf object to sp, for packages that don't yet support sf - I do this a fair bit for raster/polygon interactions. So you could do:
simplepolys <- rmapshaper::ms_simplify(input = as(sfobj, 'Spatial')) %>%
st_as_sf()
Best Answer
To drop the geometry column, use
st_drop_geometry()
:Before
st_drop_geometry()
was added to the sf package (in November, 2018), one could produce the same result using thest_set_geometry()
function, like this: