我有一个从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逻辑的最佳方法是什么?
最适合我的):
解决方法
受@ 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