[英]Extracting one parameter from each row of a dataframe containing a list within a list per row in 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.