【问题标题】:flatten a tibble in dplyr with NAs用 NAs 展平 dplyr 中的 tibble
【发布时间】:2018-07-21 22:49:37
【问题描述】:

我有以下 数据,

 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 配对,以便我可以将label 中的每个子元素与其主 ID 链接起来。我正在尝试获取带有标题的labelurlnamecolormainId

a similar question 的解决方案可以正常工作,除非NAs 嵌套在label 的子元素中

map_df(h, flatten_dfr)

bind_rows_(x, .id) 中的错误:参数 1 必须有名称

【问题讨论】:

  • 您希望删除第三个mainID,还是将NA 用于其他值?
  • @alistaire 我想要 NAs

标签: tibble tibble r dplyr flatten tibble


【解决方案1】:

您可以先过滤掉缺少labelmainIds,然后用full_join 重新添加它们(或者如果您的mainIds 是唯一的,则只需添加bind_rows)。

library(tidyverse)

h_label_missing <- h %>% 
  filter(map_lgl(label, ~all(is.na(.)))) %>% 
  select(-label)

h %>% 
  filter(!map_lgl(label, ~all(is.na(.)))) %>% 
  mutate(label = map(label, bind_rows)) %>% 
  unnest() %>% 
  full_join(h_label_missing, by = "mainId")

# 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/emergenzeHack/terremotocentro/labels/per%20sviluppatori per sviluppatori d4c5f9 F      
# 2 216226960 442034204 https://api.github.com/repos/emergenzeHack/terremotocentro/labels/sito%20principale  sito principale  5319e7 F      
# 3 215647494 442051239 https://api.github.com/repos/emergenzeHack/terremotocentro/labels/mappa              mappa            0052cc F      
# 4 215647494 431676528 https://api.github.com/repos/emergenzeHack/terremotocentro/labels/per%20sviluppatori per sviluppatori d4c5f9 F      
# 5 215647494 442034204 https://api.github.com/repos/emergenzeHack/terremotocentro/labels/sito%20principale  sito principale  5319e7 F      
# 6 242390063        NA NA                                                                                   NA               NA     NA     

【讨论】:

    【解决方案2】:

    这是一种将仅包含 NA_character_ 的元素替换为 NAs 的列表的方法,该列表的名称类似于第一行的第一个元素。之后,bind_rowsunnest 将正常工作。

    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
    

    【讨论】:

      猜你喜欢
      • 2019-11-27
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 2019-03-06
      • 1970-01-01
      相关资源
      最近更新 更多