展平递归列表

geo*_*ory 9 r

关于这个主题显然有很多问题,但我看不出任何一般的解决方案:我有一个深度递归的列表,并希望将其展平为包含所有非列表项的单个列表.

例如,采用此嵌套列表:

d = list(
  list(
    list(
      iris[sample(1:150,3),],
      iris[sample(1:150,3),]
    ),
    list(
      list(
        iris[sample(1:150,3),],
        list(
          iris[sample(1:150,3),],
          iris[sample(1:150,3),]
        )
      )
    )
  )
)
Run Code Online (Sandbox Code Playgroud)

把它变成这个:

list(iris[sample(1:150,3),],
     iris[sample(1:150,3),],
     iris[sample(1:150,3),],
     iris[sample(1:150,3),],
     iris[sample(1:150,3),])
Run Code Online (Sandbox Code Playgroud)

我根据其他解决方案尝试了以下一些方法:

purrr::flatten(d)
plyr::llply(d, unlist)
lapply(d, unlist, use.names=FALSE)
Run Code Online (Sandbox Code Playgroud)

没有达到期望的结果,在该示例中是单个列表长度5,所有项目是a data.frame.任何建议赞赏.

Cla*_*lke 10

这是仅使用基数R的一般展平函数:

flatten <- function(x) {
  if (!inherits(x, "list")) return(list(x))
  else return(unlist(c(lapply(x, flatten)), recursive = FALSE))
}
Run Code Online (Sandbox Code Playgroud)

结果:

flatten(d)
#[[1]]
#    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#44           5.0         3.5          1.6         0.6     setosa
#138          6.4         3.1          5.5         1.8  virginica
#87           6.7         3.1          4.7         1.5 versicolor
#
#[[2]]
#   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#19          5.7         3.8          1.7         0.3     setosa
#1           5.1         3.5          1.4         0.2     setosa
#71          5.9         3.2          4.8         1.8 versicolor
#
#[[3]]
#    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#31           4.8         3.1          1.6         0.2     setosa
#98           6.2         2.9          4.3         1.3 versicolor
#134          6.3         2.8          5.1         1.5  virginica
#
#[[4]]
#    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#140          6.9         3.1          5.4         2.1  virginica
#119          7.7         2.6          6.9         2.3  virginica
#57           6.3         3.3          4.7         1.6 versicolor
#
#[[5]]
#    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#73           6.3         2.5          4.9         1.5 versicolor
#54           5.5         2.3          4.0         1.3 versicolor
#146          6.7         3.0          5.2         2.3  virginica
Run Code Online (Sandbox Code Playgroud)

同理:

x <- list(list("A"), list(list("A"), list("A")))
flatten(x)
#[[1]]
#[1] "A"
#
#[[2]]
#[1] "A"
#
#[[3]]
#[1] "A"

x <- list(list(1), list(list(2), list(3)))
flatten(x)
#[[1]]
#[1] 1
#
#[[2]]
#[1] 2
#
#[[3]]
#[1] 3
Run Code Online (Sandbox Code Playgroud)

当目标是删除它们时,似乎有点圆形 - 添加更多列表,但list/ unlistroute是连接具有不同数量元素的列表的唯一可靠方法.


ali*_*ire 5

rlang::squash 非常神奇:

set.seed(47)

d = list(list(list(iris[sample(1:150,3),],
                   iris[sample(1:150,3),]),
              list(list(iris[sample(1:150,3),],
                        list(iris[sample(1:150,3),],
                             iris[sample(1:150,3),])
              ))
))

rlang::squash(d)
#> [[1]]
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 147          6.3         2.5          5.0         1.9  virginica
#> 56           5.7         2.8          4.5         1.3 versicolor
#> 113          6.8         3.0          5.5         2.1  virginica
#> 
#> [[2]]
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 124          6.3         2.7          4.9         1.8  virginica
#> 86           6.0         3.4          4.5         1.6 versicolor
#> 103          7.1         3.0          5.9         2.1  virginica
#> 
#> [[3]]
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 59          6.6         2.9          4.6         1.3 versicolor
#> 70          5.6         2.5          3.9         1.1 versicolor
#> 81          5.5         2.4          3.8         1.1 versicolor
#> 
#> [[4]]
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#> 139          6.0         3.0          4.8         1.8 virginica
#> 21           5.4         3.4          1.7         0.2    setosa
#> 104          6.3         2.9          5.6         1.8 virginica
#> 
#> [[5]]
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 25          4.8         3.4          1.9         0.2     setosa
#> 90          5.5         2.5          4.0         1.3 versicolor
#> 75          6.4         2.9          4.3         1.3 versicolor
Run Code Online (Sandbox Code Playgroud)