Trying to add a data column to a simplefeatures point object using dplyr::left_join, but it appears to drop the sf class:
class(sf)
[1] "sf" "data.frame"
class(df)
[1] "tbl_df" "tbl" "data.frame"
eg <- left_join(sf, df, by = 'common_col')
class(eg)
[1] "data.frame"
same thing happens when df
is just a plain data frame. I can fix it easily enough with
eg <- st_as_sf(eg)
but this feels like an unnecessary extra step. Am I making poor function choices for what I'm trying to do, or is this an actual bug?
Best Answer
Until this gets changed you might want to define a
left_join
method forsf
classes that does the conversion for you:Example: before:
Then with
left_join.sf
, just usingleft_join(...)
does: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 atbl
version ofx
, and then its converted back to a data frame: