简体   繁体   English

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

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

I am trying to build a dataframe of KML files.我正在尝试构建一个 dataframe 的 KML 文件。 I have 52 different files in my dataset, and I have already uploaded them to R using the following code chunk:我的数据集中有 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)

Ok, so far everything is alright.好的,到目前为止一切正常。 I have all of the 52 files uploaded in the file_contents list.我在file_contents列表中上传了所有 52 个文件。 This is the file_contents list Now, I need to get each of the 52 lists in file_contents , that contain one dataframe each, and build a single dataframe. So it should bind 52 different dataframes into a single one.这是 file_contents 列表现在,我需要获取file_contents中的 52 个列表中的每一个,每个列表包含一个 dataframe,并构建一个 dataframe。因此它应该将 52 个不同的数据帧绑定到一个数据帧中。 I`ve tried lots of different ways to solve this problem, but I always failed.我尝试了很多不同的方法来解决这个问题,但我总是失败。

Thanks for the support:)感谢您的支持:)

I tried different loops, do.call function, some native R functions, but none of them worked.我尝试了不同的循环, do.call function,一些原生的 R 函数,但都没有用。 I`d either get an error message (eg我要么收到一条错误消息(例如

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

) or just create a dataframe with the first element of the file_contents list. ) 或仅使用file_contents列表的第一个元素创建一个 dataframe。 I was expecting to get a single dataframe with the 52 dataframes binded...我期待得到一个绑定了 52 个数据帧的 dataframe ...

Have you tried?你有没有尝试过?

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

That assumes the col names are the same if they are not set use.names = F.如果未设置 use.names = F,则假设列名称相同。

You can use purrr::map on a list of files and build a single dataset if all of the files are regularly shaped (have the same columns).您可以在文件列表上使用purrr::map并构建单个数据集(如果所有文件都是规则形状的(具有相同的列))。 Below is an example using the nc dataset included with the sf package.以下是使用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...

Created on 2022-11-17 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-11-17

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

相关问题 从包含 R 中每行列表中的列表的数据帧的每一行中提取一个参数 - Extracting one parameter from each row of a dataframe containing a list within a list per row in R 在 R 中操作包含列表的数据帧的每一行 - Manipulate each Row of a Dataframe containing Lists in R 堆叠列表到 R 中的一个数据帧 - Stacked lists to one dataframe in R R如何创建包含列表列的数据框? - R How do I create a dataframe containing a column of lists? 从包含元素和空格的单个字符串创建R数据帧 - Create R dataframe from single string containing elements and whitespace 从一个 Dataframe - R 创建多个图 - Create Multiple Graphs from One Dataframe - R 如何使用 R 将 dataframe 中的两列转换为一个列表 - How to convert two column from dataframe into one list with R 如何将 Dataframe 作为 R 中的列表放入一个单元格 - How put Dataframe into one cell as list in R 我如何设计一个r函数,该函数从列表列表中选择特定元素,并返回一个数据帧作为输出 - How can I design an r function that selects specific elements from a list of lists, and returns a dataframe as an output R将两个列表合并为一个,从每个列表中交替绘制元素 - R merge two lists into one, drawing elements from each list alternatively
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM