-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge sf object and data frame [Help] #193
Comments
Both > st_geometry(merged) <- merged$geometry
> class(merged)
[1] "sf" "data.frame" If by "plots are different" you mean that your merger drops all geometries for which |
Thanks; I think we should make this automatic, by providing |
Thank you very much, all.x = TRUE did the trick to preserve the geometry. |
Would you add a |
Well, that doesn't do much, apart from suppressing a warning in case of multiple matches. My gut feeling is that it will be more useful to try to support the |
While it would be great to eventually have a set of spatial Example: library(dplyr, warn.conflicts = F, quietly = T)
library(sf)
#> Linking to GEOS 3.5.0, GDAL 2.1.1, proj.4 4.9.3
demo(nc, ask = FALSE, echo = FALSE, verbose = FALSE)
nc_df <- nc %>% unclass %>% as_data_frame() %>% select(NAME, BIR74)
nc %>% select(-BIR74) %>% left_join(nc_df, by = "NAME") %>% class
#> [1] "data.frame" |
@brendaprallon please test. |
@tiernanmartin pls test the non-spatial *_join.sf methods. Now, empty a = data.frame(a = 1:3, b = 5:7)
st_geometry(a) = st_sfc(st_point(c(0,0)), st_point(c(1,1)), st_point(c(2,2)))
b = data.frame(x = c("a", "b", "c"), b = c(2,5,6))
full_join(a, b)
# Joining, by = "b"
# Simple feature collection with 4 features and 3 fields (of which 1 is empty)
# geometry type: GEOMETRY
# dimension: XY
# bbox: xmin: 0 ymin: 0 xmax: 2 ymax: 2
# epsg (SRID): NA
# proj4string: NA
# a b x geometry
# 1 1 5 b POINT(0 0)
# 2 2 6 c POINT(1 1)
# 3 3 7 <NA> POINT(2 2)
# 4 NA 2 a GEOMETRYCOLLECTION() |
@tiernanmartin 3f7c25d now adds |
@edzer my limited tests of the non-spatial |
I tried library(sf)
#> Linking to GEOS 3.5.0, GDAL 2.1.1, proj.4 4.9.3
a = data.frame(a = 1:3, b = 5:7)
st_geometry(a) = st_sfc(st_point(c(0, 0)), st_point(c(1, 1)), st_point(c(2,
2)))
b = data.frame(x = c("a", "b", "c"), b = c(2, 5, 6))
st_geometry(b) = st_sfc(st_point(c(0, 0)), st_point(c(1, 1)), st_point(c(2,
2)))
st_join(a, b)
#> Error: length(setdiff(names(value), nv)) == 0 is not TRUE Running
|
@edzer Thank you so much for the quick response. However I'm running into a lot of trouble to install the github version for some reason. I'll test it as soon as I get around that. Thanks! |
Things should work now with 0.3-4 on CRAN; could you pls check? |
Thank you, I tested it with my data and it is working perfectly! |
Hi, I'm sorry if this issue doesn't apply, but I wasn't sure where else to ask.
I'm having trouble merging a
sf
object and adata frame
by a common column i.e "NAME". The final result is adata frame
instead ofsf
, which I didn't expect since I did not change anything in the geometry. As I try to convert it back tosf
withst_as_sf()
, the geometry appears to have changed, because the plot is different. Here's the example I constructed bellow:Is there a simple solution to this or am I doing something wrong? When I try merging these using a
SpatialPolygonsDataFrame
object and adata.frame
, it works. Thank you so much!The text was updated successfully, but these errors were encountered: