gpt4 book ai didi

r - 如何使用 full_join 将 sf map 数据与 tibble 数据连接起来?

转载 作者:行者123 更新时间:2023-12-02 00:04:31 25 4
gpt4 key购买 nike

我正在尝试使用 tibble 连接来自 rnaturalearth 的 map 数据。

这是我的 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"))

这是我使用的代码:

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")

我收到这个错误:

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.

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

最佳答案

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

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

解决方法是:

library(sf)

关于r - 如何使用 full_join 将 sf map 数据与 tibble 数据连接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61052904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com