【发布时间】:2021-12-28 21:45:11
【问题描述】:
我有一个嵌套的列表列表,其中包含一些数据框。但是,数据框可以出现在列表中的任何级别。我想要结束的是一个平面列表,即只有一个级别,其中每个元素只有数据框,而所有其他东西都被丢弃了。
我为此想出了一个解决方案,但它看起来很笨拙,我相信应该有一个更优雅的解决方案。
重要的是,我正在寻找基础 R 中的东西,它可以在嵌套列表中的任何级别提取数据帧。我尝试过unlist() 并涉足rapply(),但不知何故没有找到令人满意的解决方案。
示例代码如下:示例列表、我实际想要实现的目标以及我自己不太满意的解决方案。感谢您的帮助!
# extract dfs from list
# example of multi-level list with some dfs in it
# note, dfs could be nested at any level
problem1 <- list(x1 = 1,
x2 = list(
x3 = "dog",
x4 = data.frame(cats = c(1, 2),
pigs = c(3, 4))
),
x5 = data.frame(sheep = c(1,2,3),
goats = c(4,5,6)),
x6 = list(a = 2,
b = "c"),
x7 = head(cars,5))
# want to end up with flat list like this (names format is optional)
result1 <- list(x2.x4 = data.frame(cats = c(1, 2),
pigs = c(3, 4)),
x5 = data.frame(sheep = c(1,2,3),
goats = c(4,5,6)),
x7 = head(cars,5))
# my solution (not very satisfactory)
exit_loop <- FALSE
while(exit_loop == FALSE){
# find dfs (logical)
idfs <- sapply(problem1, is.data.frame)
# check if all data frames
exit_loop <- all(idfs)
# remove anything not df or list
problem1 <- problem1[idfs | sapply(problem1, is.list)]
# find dfs again (logical)
idfs <- sapply(problem1, is.data.frame)
# unlist only the non-df part
problem1 <- c(problem1[idfs], unlist(problem1[!idfs], recursive = FALSE))
}
【问题讨论】:
标签: r