哪个是使用tidyverse工具压缩从关系数据库派生的嵌套列表的最佳方法?

rgu*_*avs 7 r nested-lists purrr tidyverse

我有一个从REST调用收到的嵌套列表.响应包括来自底层关系数据库的嵌套列表集.我想将列表展平以简化分析.我试图遵循purrr教程中的指导原则,但我无法让它工作.

我的简化输入

hist1 <- list(field="type", from_string ="issue", to_string="bug")
hist2 <- list(field="status", from_string ="open", to_string="closed")
hist3 <- list(field="type", from_string ="bug", to_string="issue")
issue1 <- list(id="123", created = "2017-11-08", issue_history = list(hist1, hist2))
issue2 <- list(id="124", created = "2017-11-10", issue_history = list(hist1, hist3))
issue <- list(issue1, issue2)
Run Code Online (Sandbox Code Playgroud)

我正在寻找扁平的输出:

id  created    type   from_string  to_string
123 2017-11-08 type   issue        bug
123 2017-11-08 status open         closed
123 2017-11-10 type   bug          issue
Run Code Online (Sandbox Code Playgroud)

为此构建scable逻辑的最佳方法是什么?

最适合我的):

  • 来自tidyverse的工具
  • 易于维护的代码
  • 不必扩展数百万个问题,即性能和内存不是关键要素

Psi*_*dom 6

受@ Nate回答启发的另一个解决方案:

map_df(issue, as_tibble) %>% 
    mutate(issue_history = map(issue_history, as_tibble)) %>% 
    unnest()

# A tibble: 4 x 5
#     id    created  field from_string to_string
#  <chr>      <chr>  <chr>       <chr>     <chr>
#1   123 2017-11-08   type       issue       bug
#2   123 2017-11-08 status        open    closed
#3   124 2017-11-10   type       issue       bug
#4   124 2017-11-10   type         bug     issue
Run Code Online (Sandbox Code Playgroud)


Nat*_*ate 5

不确定是否有更多purrr方法可以做到这一点,但它确实有效.

library(tidyverse)

map(issue, as.tibble) %>%
  map_df(~ rowwise(.) %>%
           mutate(issue_history = list(bind_rows(issue_history))) %>%
           unnest() )

# A tibble: 4 x 5
     id    created  field from_string to_string
  <chr>      <chr>  <chr>       <chr>     <chr>
1   123 2017-11-08   type       issue       bug
2   123 2017-11-08 status        open    closed
3   124 2017-11-10   type       issue       bug
4   124 2017-11-10   type         bug     issue
Run Code Online (Sandbox Code Playgroud)