包含 sf 对象的 unnest 列表列
unnest list columns containing sf objects
我正在尝试 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 读入 common table 和 unnest。
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
编辑:我的目标是扩展列表列 data
中的所有 sf
个对象,以便它们的所有列都出现在新的 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
我正在尝试 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 读入 common table 和 unnest。
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
编辑:我的目标是扩展列表列 data
中的所有 sf
个对象,以便它们的所有列都出现在新的 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