我有一个很大的shapefile,需要按属性(分组值)将其拆分成几个。在ArcGIS中,此函数调用Split By Attribute
,以Split By Attribute
库为例,读取nc
数据帧
sf
我想根据library(tidyverse)
library(sf)
nc = st_read(system.file("shape/nc.shp", package="sf"))
nc
Simple feature collection with 100 features and 14 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
epsg (SRID): 4267
proj4string: +proj=longlat +datum=NAD27 +no_defs
First 10 features:
AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID
1 0.114 1.442 1825 1825 Ashe 37009 37009 5
2 0.061 1.231 1827 1827 Alleghany 37005 37005 3
3 0.143 1.630 1828 1828 Surry 37171 37171 86
变量将其分割为100个shapefile。所以我要像这样进入NAME
,group_by
和nest
。
walk
这个漂亮的管道向我返回了以下错误:
错误:无法将
nc %>% mutate(group = as.character(NAME)) %>% group_by(group) %>% nest() %>% mutate(data = map(data, ~st_as_sf(.x))) %>% walk2(.x = data, .y = group, ~st_write(obj = .x, dsn = paste0(.y, ".shp")))
对象转换为函数
grouped_df/tbl_df/tbl/data.frame