我怎么写一个purrr :: keep的递归版本?

问题描述 投票:5回答:1

假设我有一个嵌套列表,其中包含不同级别的一堆数据帧。我想提取出仅数据框的扁平列表。我怎么用purrr函数写这个?我应该看看reduce吗?

例如,给定数据:

s <- list(x = 1:10,
          data = data.frame(report = LETTERS[1:5],
                            value = rnorm(5, 20, 5)),
          report = list(A = data.frame(x = 1:3, y = c(2, 4, 6)),
                        B = data.frame(x = 1:3, y = c(3, 6, 9)),
                        z = 4:10,
                        other = data.frame(w = 3:5,
                                       color = c("red", "green", "blue"))))

我想要返回的功能:

list(data = data.frame(report = LETTERS[1:5],
                       value = rnorm(5, 20, 5)),
     `report$A` = data.frame(x = 1:3, y = c(2, 4, 6)),
     `report$B` = data.frame(x = 1:3, y = c(3, 6, 9)),
     `report$other` = data.frame(w = 3:5,
                                 color = c("red", "green", "blue")))

我写了一个递归函数:

recursive_keep <- function(.x, .f) {
  loop <- function(.y) {
    if(is.list(.y)) {
      c(keep(.y, .f), flatten(map(discard(.y, .f), loop)))
    } else if(.f(.y)) {
      .y
    } else {
      NULL
    }
  }
  loop(.x)
}

它可以被称为:

recursive_keep(s, is.data.frame)

它似乎适用于此示例,但它不保留名称信息。我希望保留足够的信息,以便从原始对象中提取数据。也许这是一个更容易回答的问题?

r purrr
1个回答
3
投票

这个带有单行主体的递归函数保留了名称并且不使用任何包:

rec <- function(x, FUN = is.data.frame)
  if (FUN(x)) list(x) else if (is.list(x)) do.call("c", lapply(x, rec, FUN))

str(rec(s))  # test

给予(在输出后继续):

List of 4
 $ data        :'data.frame':   5 obs. of  2 variables:
  ..$ report: Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5
  ..$ value : num [1:5] 29.1 19.9 21.2 13 25.2
 $ report.A    :'data.frame':   3 obs. of  2 variables:
  ..$ x: int [1:3] 1 2 3
  ..$ y: num [1:3] 2 4 6
 $ report.B    :'data.frame':   3 obs. of  2 variables:
  ..$ x: int [1:3] 1 2 3
  ..$ y: num [1:3] 3 6 9
 $ report.other:'data.frame':   3 obs. of  2 variables:
  ..$ w    : int [1:3] 3 4 5
  ..$ color: Factor w/ 3 levels "blue","green",..: 3 2 1

关于从原始对象A获取report中的s

s[["report"]][["A"]]

要么

ix <- c("report", "A")
s[[ix]]
© www.soinside.com 2019 - 2024. All rights reserved.