【发布时间】: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