Skip to content
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

Joins between data.frames and sf objects #2833

Closed
Nowosad opened this issue Jun 2, 2017 · 3 comments
Closed

Joins between data.frames and sf objects #2833

Nowosad opened this issue Jun 2, 2017 · 3 comments

Comments

@Nowosad
Copy link

Nowosad commented Jun 2, 2017

Second part of this issue - r-spatial/sf#372

Lately I've been trying to use joins for pairs of sf objects and data.frames and I came across a problematic joins. When a data.frame is the main object in join., then a new object has a geometry column, but doesn't have a sf class.
My expectation is that a geom column should be removed in these cases. I'm not sure, if my expectation is the best. What do you think?

library(tidyverse)
library(sf)

sf_obj = st_read(system.file("shape/nc.shp", package = "sf")) %>% filter(NAME %in% 
  c("Ashe", "Surry")) %>% select(NAME)
#> Reading layer `nc' from data source `/home/jn/R/x86_64-redhat-linux-gnu-library/3.3/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs

df_obj = data.frame(NAME = c("Ashe", "Surry", "Rowan"), VALUE = c(1, 4, 6))

left_join1 = df_obj %>% left_join(sf_obj, by = "NAME")
#> Warning: Column `NAME` joining factors with different levels, coercing to
#> character vector
left_join1
#>    NAME VALUE                       geometry
#> 1  Ashe     1 MULTIPOLYGON(((-81.47275543...
#> 2 Surry     4 MULTIPOLYGON(((-80.45634460...
#> 3 Rowan     6                           NULL

right_join2 = df_obj %>% right_join(sf_obj, by = "NAME")
#> Warning: Column `NAME` joining factors with different levels, coercing to
#> character vector
right_join2
#>    NAME VALUE                       geometry
#> 1  Ashe     1 MULTIPOLYGON(((-81.47275543...
#> 2 Surry     4 MULTIPOLYGON(((-80.45634460...

inner_join1 = df_obj %>% inner_join(sf_obj, by = "NAME")
#> Warning: Column `NAME` joining factors with different levels, coercing to
#> character vector
inner_join1
#>    NAME VALUE                       geometry
#> 1  Ashe     1 MULTIPOLYGON(((-81.47275543...
#> 2 Surry     4 MULTIPOLYGON(((-80.45634460...

full_join2 = df_obj %>% full_join(sf_obj, by = "NAME")
#> Warning: Column `NAME` joining factors with different levels, coercing to
#> character vector
full_join2
#>    NAME VALUE                       geometry
#> 1  Ashe     1 MULTIPOLYGON(((-81.47275543...
#> 2 Surry     4 MULTIPOLYGON(((-80.45634460...
#> 3 Rowan     6                           NULL
@hadley
Copy link
Member

hadley commented Jun 13, 2017

Could you please use the reprex package so I can see the outputs?

@hadley hadley added the reprex needs a minimal reproducible example label Jun 13, 2017
@Nowosad
Copy link
Author

Nowosad commented Jun 13, 2017

Yes, of course. I've updated my question.

@hadley hadley added data frame feature a feature request or enhancement and removed reprex needs a minimal reproducible example feature a feature request or enhancement labels Jun 13, 2017
@hadley
Copy link
Member

hadley commented Jun 13, 2017

Keeping the geometry column seems like the right behaviour to me.

@hadley hadley closed this as completed Jun 13, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants