我有以下tibble数据,
h <- structure(list(label = list(list(structure(list(id = 431676528L,
url = "https://api.github.com/repos/emergenzeHack/terremotocentro/labels/per%20sviluppatori",
name = "per sviluppatori", color = "d4c5f9", default = FALSE), .Names = c("id",
"url", "name", "color", "default")), structure(list(id = 442034204L,
url = "https://api.github.com/repos/emergenzeHack/terremotocentro/labels/sito%20principale",
name = "sito principale", color = "5319e7", default = FALSE), .Names = c("id",
"url", "name", "color", "default"))), list(structure(list(id = 442051239L,
url = "https://api.github.com/repos/emergenzeHack/terremotocentro/labels/mappa",
name = "mappa", color = "0052cc", default = FALSE), .Names = c("id",
"url", "name", "color", "default")), structure(list(id = 431676528L,
url = "https://api.github.com/repos/emergenzeHack/terremotocentro/labels/per%20sviluppatori",
name = "per sviluppatori", color = "d4c5f9", default = FALSE), .Names = c("id",
"url", "name", "color", "default")), structure(list(id = 442034204L,
url = "https://api.github.com/repos/emergenzeHack/terremotocentro/labels/sito%20principale",
name = "sito principale", color = "5319e7", default = FALSE), .Names = c("id",
"url", "name", "color", "default"))), list(NA_character_)), mainId = c("216226960",
"215647494", "242390063")), .Names = c("label", "mainId"), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame"))
我希望将标签中的值与mainId
配对,以便我可以将labe
l中的每个子元素与其主ID 相关联。我正在尝试使用标题为tibble:label
,url
,name
,color
,mainId
。
a similar question的解决方案正常工作,除非NA
label
map_df(h, flatten_dfr)
bind_rows_(x,.id)出错:参数1必须具有名称
答案 0 :(得分:2)
您可以先删除遗失mainId
的{{1}},然后将其添加回label
(如果full_join
,则只需bind_rows
s是独一无二的。)
mainId
答案 1 :(得分:2)
这是一种方法,它将包含NA_character_
的元素替换为NA
列表,其名称与第一行的第一个元素相同。在此之后,bind_rows
和unnest
将正常工作。
library(tidyverse)
nested_names <- names(pluck(h, 'label', 1, 1))
h2 <- h %>%
mutate(label = map(label, map_if,
~is.null(names(.x)),
~setNames(rep(list(NA), length(nested_names)),
nested_names)),
label = map(label, bind_rows)) %>%
unnest()
h2
#> # A tibble: 6 x 6
#> mainId id url name color default
#> <chr> <int> <chr> <chr> <chr> <lgl>
#> 1 216226960 431676528 https://api.github.com/repos… per svi… d4c5… FALSE
#> 2 216226960 442034204 https://api.github.com/repos… sito pr… 5319… FALSE
#> 3 215647494 442051239 https://api.github.com/repos… mappa 0052… FALSE
#> 4 215647494 431676528 https://api.github.com/repos… per svi… d4c5… FALSE
#> 5 215647494 442034204 https://api.github.com/repos… sito pr… 5319… FALSE
#> 6 242390063 NA <NA> <NA> <NA> NA