繁体   English   中英

如何从一个列表中创建一个 dataframe,该列表的元素是包含一个 dataframe 的列表,每个 R

[英]How to create one dataframe from a list whose elements are lists containing one dataframe each in R

我正在尝试构建一个 dataframe 的 KML 文件。 我的数据集中有 52 个不同的文件,我已经使用以下代码块将它们上传到 R:

#importing data
library(fs)
file_paths = fs::dir_ls("C:/Users/JoaoArbache/Desktop/Mestrado/carbono/dados")
file_contents = list()

for(i in seq_along(file_paths)) {
  file_contents[[i]] = st_read(
    dsn  = file_paths[[i]]
  )
}

#renaming the lists
numeros = list()
for(i in file_paths) {
  numeros[[i]] = str_extract(i, "\\d+") %>% 
                   as.numeric()
}
id = do.call(rbind.data.frame, numeros) %>% 
    filter(!row_number() %in% c(53))
colnames(id)[1] = "id"

file_contents = set_names(file_contents, id$id)

好的,到目前为止一切正常。 我在file_contents列表中上传了所有 52 个文件。 这是 file_contents 列表现在,我需要获取file_contents中的 52 个列表中的每一个,每个列表包含一个 dataframe,并构建一个 dataframe。因此它应该将 52 个不同的数据帧绑定到一个数据帧中。 我尝试了很多不同的方法来解决这个问题,但我总是失败。

感谢您的支持:)

我尝试了不同的循环, do.call function,一些原生的 R 函数,但都没有用。 我要么收到一条错误消息(例如

Error in `[[<-`(`*tmp*`, i, value = as.data.frame(i)) : 
  attempt to select more than one element in vectorIndex

) 或仅使用file_contents列表的第一个元素创建一个 dataframe。 我期待得到一个绑定了 52 个数据帧的 dataframe ...

你有没有尝试过?

library(data.table)
rbindlist(file_contents, use.names = T, fill = T)

如果未设置 use.names = F,则假设列名称相同。

您可以在文件列表上使用purrr::map并构建单个数据集(如果所有文件都是规则形状的(具有相同的列))。 以下是使用sf package 中包含的nc数据集的示例。

library(sf)
library(dplyr) 
library(purrr)

# make a temporary directory for the example
temp_dir <- tempdir()

# read nc data 
nc <- st_read(system.file("shape/nc.shp", package="sf"))

# create two datasets with all the same columns, but different data
one <- nc[1:3,] 
two <- nc[55,]

# write two separate kml objects to disk
st_write(one, paste0(temp_dir, "/", "one.kml"))
#> Writing layer `one' to data source `/tmp/RtmpfRjSGc/one.kml' using driver `KML'
#> Writing 3 features with 14 fields and geometry type Multi Polygon.
st_write(two, paste0(temp_dir, "/", "two.kml"))
#> Writing layer `two' to data source `/tmp/RtmpfRjSGc/two.kml' using driver `KML'
#> Writing 1 features with 14 fields and geometry type Multi Polygon.

# show the files on disk, just for illustration
list.files(path = temp_dir, pattern = "*.kml", full.names = T)
#> [1] "/tmp/RtmpfRjSGc/one.kml" "/tmp/RtmpfRjSGc/two.kml"

# read the two files & make them one dataframe:
together <- temp_dir %>%
  list.files(pattern = "*.kml", full.names = T) %>%
  map_dfr(st_read)
#> Reading layer `one' from data source `/tmp/RtmpfRjSGc/one.kml' using driver `LIBKML'
#> Simple feature collection with 3 features and 24 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -81.74091 ymin: 36.23402 xmax: -80.43509 ymax: 36.58977
#> Geodetic CRS:  WGS 84
#> Reading layer `two' from data source `/tmp/RtmpfRjSGc/two.kml' using driver `LIBKML'
#> Simple feature collection with 1 feature and 24 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -83.259 ymin: 35.29087 xmax: -82.74374 ymax: 35.79195
#> Geodetic CRS:  WGS 84

head(together)
#> Simple feature collection with 4 features and 24 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -83.259 ymin: 35.29087 xmax: -80.43509 ymax: 36.58977
#> Geodetic CRS:  WGS 84
#>        Name description timestamp begin  end altitudeMode tessellate extrude
#> 1      Ashe        <NA>      <NA>  <NA> <NA>         <NA>         -1       0
#> 2 Alleghany        <NA>      <NA>  <NA> <NA>         <NA>         -1       0
#> 3     Surry        <NA>      <NA>  <NA> <NA>         <NA>         -1       0
#> 4   Haywood        <NA>      <NA>  <NA> <NA>         <NA>         -1       0
#>   visibility drawOrder icon  AREA PERIMETER CNTY_ CNTY_ID  FIPS FIPSNO CRESS_ID
#> 1         -1        NA <NA> 0.114     1.442  1825    1825 37009  37009        5
#> 2         -1        NA <NA> 0.061     1.231  1827    1827 37005  37005        3
#> 3         -1        NA <NA> 0.143     1.630  1828    1828 37171  37171       86
#> 4         -1        NA <NA> 0.144     1.690  1996    1996 37087  37087       44
#>   BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79                       geometry
#> 1  1091     1      10  1364     0      19 MULTIPOLYGON (((-81.47258 3...
#> 2   487     0      10   542     3      12 MULTIPOLYGON (((-81.2397 36...
#> 3  3188     5     208  3616     6     260 MULTIPOLYGON (((-80.45612 3...
#> 4  2110     2      57  2463     8      62 MULTIPOLYGON (((-82.74374 3...

reprex package (v2.0.1) 创建于 2022-11-17

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM