首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Purrr flatten_dfr

Purrr flatten_dfr
EN

Stack Overflow用户
提问于 2020-10-04 02:27:42
回答 2查看 218关注 0票数 1

我正在试图提取包含坐标信息的列表到一个sf数据框架中。

这是清单

代码语言:javascript
复制
feature <- list(type = "Feature", properties = list(`_leaflet_id` = 26065L, 
feature_type = "polyline"), geometry = list(type = "LineString", 
coordinates = list(list(-74.210091, 40.382121), list(-73.942375, 
    40.661889))))

我想把它转换成三列的sf数据(leaflet_id,feature_type,几何学)

我尝试过使用purrr::flatten_dfr(特性),但得到了错误:参数1必须有名称。

我已经复习过其他类似于下面的帖子,这些帖子看上去很有希望,但没有起作用。

谢谢

Error in bind_rows_(x, .id) : Argument 1 must have names

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-04 07:57:47

您的feature列表看起来像使用类似于jsonlite::fromJSON()的东西读取geojson的结果。

如果您使用geojson直接读取sf::st_read(),您将得到sf对象。

要按原样修复代码,您可以这样做。

代码语言:javascript
复制
library(jsonlite)
library(sf)

sf <- jsonlite::toJSON( feature, auto_unbox = TRUE ) %>%
    sf::st_read()

sf
# Simple feature collection with 1 feature and 2 fields
# geometry type:  LINESTRING
# dimension:      XY
# bbox:           xmin: -74.21009 ymin: 40.38212 xmax: -73.94238 ymax: 40.66189
# z_range:        zmin: NA zmax: NA
# m_range:        mmin: NA mmax: NA
# geographic CRS: WGS 84
# _leaflet_id feature_type                       geometry
# 1       26065     polyline LINESTRING (-74.21009 40.38...
票数 1
EN

Stack Overflow用户

发布于 2020-10-04 03:46:16

您的列表中有两个问题会引发flatten_dfr错误。

  1. 并不是所有嵌套列表都命名的。coordinates中的两个列表需要名称。
  2. 您需要使用列表名type两次。这是行不通的,因为flatten之后的数据框架将有两个名称相同的列.

如果您修复了这些问题,flatten_dfr将毫无错误地运行:

代码语言:javascript
复制
feature <- list(type = "Feature", 
                properties = list(`_leaflet_id` = 26065L, 
                                  feature_type = "polyline"), 
                geometry = list(type1 = "LineString", 
                                coordinates = list(foo=list(-74.210091, 40.382121), 
                                                   bar=list(-73.942375, 
 
flatten_dfr(feature)
# A tibble: 2 x 5
  type    `_leaflet_id` feature_type type1      coordinates 
  <chr>           <int> <chr>        <chr>      <named list>
1 Feature         26065 polyline     LineString <list [2]>  
2 Feature         26065 polyline     LineString <list [2]>  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64190647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档