gpt4 book ai didi

r - 将 data.frame 转换为 SpatialPolygonsDataFrame

转载 作者:行者123 更新时间:2023-12-04 13:18:04 27 4
gpt4 key购买 nike

以下是用于重现目的的数据:

  structure(list(countyfp10 = c(1, 1, 1, 1, 3, 3, 3, 3, 5, 5, 5, 
5, 7, 7, 7, 7), id = c(7417, 7418, 7419, 7420, 7421, 7422, 7423,
7424, 7425, 7426, 7427, 7428, 7429, 7430, 7431, 7432), lat = c(39.4797245,
39.5544678, 39.4681687, 39.199806, 39.4017623, 39.3093943, 39.4272021,
39.5618129, 39.7934997, 39.4835134, 39.4989196, 39.4819145, 39.4727694,
39.4675515, 39.4693146, 39.4644503), long = c(-118.7908571, -118.8095638,
-118.8195712, -118.5429041, -118.754186, -118.8861865, -118.9729817,
-117.9418517, -118.9516281, -118.8487913, -119.0205114, -118.7695846,
-118.7938896, -118.76011, -118.7778707, -118.7902103)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -16L), spec = structure(list(
cols = list(countyfp10 = structure(list(), class = c("collector_double",
"collector")), id = structure(list(), class = c("collector_double",
"collector")), lat = structure(list(), class = c("collector_double",
"collector")), long = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))

这是当前存储为 data.frame但我想把它转换成 SpatialPolygonsDataFrame .最好的方法是什么?

最佳答案

我开发了 sfheaders图书馆为此目的。

devtools::install_github("dcooley/sfheaders")
library(sfheaders)

sf <- sfheaders::sf_polygon(
obj = df
, x = "long"
, y = "lat"
, polygon_id = "countyfp10"
)
并在传单中展示它的工作原理(其他 plotting libraries 可用;))
library(leaflet)
leaflet() %>%
addTiles() %>%
addPolygons(data = sf)
enter image description here

关于r - 将 data.frame 转换为 SpatialPolygonsDataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57763766/

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