【问题标题】:How to create one dataframe from a list whose elements are lists containing one dataframe each in R如何从一个列表创建一个数据框,该列表的元素是列表,每个列表在 R 中包含一个数据框
【发布时间】:2022-11-18 01:10:43
【问题描述】:

我正在尝试构建 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)

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

感谢您的支持 :)

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

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

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

【问题讨论】:

  • 试试dplyr::bind_rows()

标签: r list dataframe transform sf


【解决方案1】:

你有没有尝试过?

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

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

【讨论】:

  • 我做了,但我收到了这条错误消息:``` rbindlist(file_contents, use.names = T, fill = T) 中的错误:第 2 列第 3 列的类属性与第 1 列的第 3 列不匹配。`` ` 有些属性是多边形,有些是多边形,所以这把事情搞砸了。
  • @jparbache - 你能包括你的列表的一小部分,其中包括上述错误和你到目前为止遇到的任何其他错误吗?
  • 列表的一小部分是什么?你要数据?这是一个带有小样本的链接:drive.google.com/drive/folders/…
  • stackoverflow.com/help/minimal-reproducible-example 你可以参考这个例子。基本上只是使用 dput 或一些构建列表基本结构的代码提供数据集的 sn-p,这会出错
【解决方案2】:

您可以在文件列表上使用 purrr::map 并构建单个数据集,如果所有文件都是规则形状(具有相同的列)。下面是一个使用 sf 包中包含的 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

【讨论】:

    猜你喜欢
    • 2020-05-26
    • 2017-01-12
    • 2023-02-23
    • 1970-01-01
    • 2021-04-09
    • 2017-04-30
    • 2021-09-23
    • 2017-01-31
    • 1970-01-01
    相关资源
    最近更新 更多