微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个从REST调用收到的嵌套列表.响应包括来自底层关系数据库的嵌套列表集.我想压扁列表以简化分析.我曾尝试遵循 purrr tutorial中的指导原则,但我无法让它发挥作用.

我的简化输入

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",hist3))
issue <- list(issue1,issue2)

我正在寻找扁平的输出

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

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

最适合我的):

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

解决方法

受@ 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

原文地址:https://www.jb51.cc/mssql/77168.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐