如何使用 full_join 将 sf 地图数据与 tibble 数据连接起来?

Rob*_*röm 2 r r-sf

我正在尝试将 rnaturalearth 的地图数据与 tibble 结合起来。

这是我的一小块的 dput:

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"))
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的代码:

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") 
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

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.
Run Code Online (Sandbox Code Playgroud)

我知道我之前已经运行过这段代码并且它有效。我最近刚刚做了一些更新,也许这可能会影响这段代码。如果有人知道这里发生了什么,将不胜感激。

小智 8

我最近也遇到了这个问题。

这是因为tidyverse(base on tibble) 无法识别sfc_MULTIPOLYGON/sfc对象。

解决办法是:

library(sf)
Run Code Online (Sandbox Code Playgroud)