我正在尝试 tidyr::unnest
包含 sf
的数据框列表列对象。
例如,关注此博客:http://r.iresmi.net/2019/03/27/open-and-merge-multiple-shapefiles/
下载形状文件
library(tidyverse)
library(sf)
library(fs)
library(httr)
library(leaflet)
# https://fr.actualitix.com/blog/shapefiles-des-departements-de-france.html
url <- c("https://fr.actualitix.com/blog/actgeoshap/73-savoie.zip",
"https://fr.actualitix.com/blog/actgeoshap/74-haute-savoie.zip")
dep <- str_extract(url, "\\d{2}.*$")
list(url, dep) %>%
pwalk(~ GET(.x, write_disk(.y)))
walk(dep, unzip, junkpaths = TRUE, exdir = "shp")
将 shapefile 读入公用表并解除嵌套。
res <- dir_ls("shp", glob = "*.shp") %>%
tibble(fname = .) %>%
mutate(data = map(fname, read_sf)) %>%
unnest(data) %>%
st_as_sf() %>%
st_set_crs(2154)
但是,这会产生错误
Error: No common type for `..1$data$geometry` <sfc_POLYGON> and `..2$data$geometry` <sfc_MULTIPOLYGON>.
可以tidyr::unnest
可以这样使用吗?
编辑:使用sf
v 0.8-0
编辑:我的目标是为了所有人 sf
列表列中的对象 data
进行扩展,以便其所有列都出现在新的 sf
中数据框。即
sf1 <- dir_ls("shp", glob = "*.shp") %>%
tibble(fname = .) %>%
mutate(data = map(fname, read_sf)) %>%
pluck(2,1)
sf2 <- dir_ls("shp", glob = "*.shp") %>%
tibble(fname = .) %>%
mutate(data = map(fname, read_sf)) %>%
pluck(2,2)
rbind(sf1, sf2)
最佳答案
是的,这似乎是 tidyr
新版本 (1.0) 之后的一个新错误(可能是因为 vctrs
包)。
请参阅sf issues #1172
同时您可以使用:
dir_ls("shp", glob = "*.shp") %>%
map(read_sf) %>%
do.call(rbind, .)
(如果 daframes 结构相同)
应该可以再次使用 sf 版本 0.8-1 2020-01-28
关于r - 取消嵌套包含 sf 对象的列表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58272062/