本文介绍了将sf对象列表转换为一个sf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个sf对象的列表,我想对其进行行绑定以创建单个sf对象.我正在寻找类似于data.table::rbindlist的功能,该功能可以有效地堆叠单个对象.

I have a list of sf objects that I would like to row bind to create a single sf object. I'm looking for a function similar to data.table::rbindlist, that would stack the individual objects in an efficient manner.

my_list <- structure(list(structure(list(idhex = 4L, geometry = structure(list(
            structure(c(664106.970004623, 6524137.38910266), class = c("XY",
            "POINT", "sfg"))), class = c("sfc_POINT", "sfc"), precision = 0, bbox = structure(c(xmin = 664106.970004623,
            ymin = 6524137.38910266, xmax = 664106.970004623, ymax = 6524137.38910266
            ), class = "bbox"), crs = structure(list(epsg = 32633L, proj4string = "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs"), class = "crs"), n_empty = 0L)), row.names = 1L, class = c("sf",
            "data.frame"), sf_column = "geometry", agr = structure(c(idhex = NA_integer_), .Label = c("constant",
            "aggregate", "identity"), class = "factor")), structure(list(
            idhex = 9, geometry = structure(list(structure(c(665491.220375992,
            6525002.7560692), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT",
            "sfc"), precision = 0, bbox = structure(c(xmin = 665491.220375992,
            ymin = 6525002.7560692, xmax = 665491.220375992, ymax = 6525002.7560692
            ), class = "bbox"), crs = structure(list(epsg = 32633L, proj4string = "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs"), class = "crs"), n_empty = 0L)), row.names = 1L, class = c("sf",
            "data.frame"), sf_column = "geometry", agr = structure(c(idhex = NA_integer_), .Label = c("constant",
            "aggregate", "identity"), class = "factor"))), .Dim = 1:2, .Dimnames = list(
            ".", NULL))

请注意,data.tablesf库尚未完全兼容.因此,rbindlist函数将返回一个未被识别为`sf对象的对象.

Note that data.table and sf libraries are not entirely compatible yet. So the rbindlist function returns an object that is not recognized as an `sf object.

single_sf <- rbindlist(my_list)
class(single_sf)

推荐答案

df <- do.call(rbind, my_list)

> class(df)
[1] "sf"         "data.frame"

值得注意的是,dplyr::bind_rowspurrr::map_dfr不适用于sf对象,因此在这种情况下rbind更好.

It is worth noting that dplyr::bind_rows and purrr::map_dfr does not work with sf objects, and thus rbind is better in this case.

这篇关于将sf对象列表转换为一个sf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 16:22