Robin Lindström
Robin Lindström

Reputation: 682

How to join a sf map data with tibble data using full_join?

I am trying to join map data from rnaturalearth with a tibble.

Here is a dput of a chunk of my tibble:

structure(list(iso3_code = c("AFG", "AFG", "ALB", "ALB", "DZA", 
"ASM"), country = c("Afghanistan", "Afghanistan", "Albania", 
"Albania", "Algeria", "American Samoa"), item = c("Maize", "Sugar cane", 
"Maize", "Soybeans", "Maize", "Sugar cane"), value = c(106670, 
25421, 391104, 744, 4142, 30)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

Here is the code I am using:

library(rnaturalearth)
world <- ne_countries(scale = "small", returnclass = "sf")  
world_filtered = world %>% 
    select(country = name_long, iso3_code = iso_a3, geometry) %>%
    filter(!is.na(iso3_code)) %>% 
    filter(country != "Antarctica") 

map = full_join(world_filtered, country_data, by = "iso3_code") 

I get this error:

Error: All columns in a tibble must be vectors.
x Column `geometry` is a `sfc_MULTIPOLYGON/sfc` object.
Run `rlang::last_error()` to see where the error occurred.

I know I have run this code earlier and it worked. I just made some updates recently and maybe that could have affected this code. Would appreciate some help if someone knows what is going on here.

Upvotes: 3

Views: 2714

Answers (2)

Jingxin Zhang
Jingxin Zhang

Reputation: 244

I also have encountered this problem recently.

This is because tidyverse (base on tibble) can't recognize sfc_MULTIPOLYGON/sfc object.

The solution is:

library(sf)

Upvotes: 9

fairground
fairground

Reputation: 61

Note: See my comment below

If you've updated/installed the tibble package since June 2019, that might be the issue. I had a similar problem with doing a spatial join and came across a tidyverse GitHub issue where gregmacfarlane surmised that this issue is caused by the newest version of tibble. Sure enough, after installing tibble version 2.1.3, I was able to do a spatial join. Previously I had installed version 3.0.1. I installed the older version using its url from the CRAN repository:

install.packages("https://cran.r-project.org/src/contrib/Archive/tibble/tibble_2.1.3.tar.gz",repos=NULL, type="source")

Upvotes: 1

Related Questions