【问题标题】:Tidyr unnest function doesn't seem to do its workTidyr unnest 功能似乎没有发挥作用
【发布时间】:2018-10-09 13:29:36
【问题描述】:

我正在尝试取消列出每行仅包含一个 url 的列表列。

我创建了一个数据框,其中包含目录中某些文件的文件元数据。这是我的代码:

df <- files_small %>% 
  keep(has_xattrs) %>% 
  set_names(basename(.)) %>% 
  map_df(read_xattrs, .id = "file") %>% 
  filter(name == "com.apple.metadata:kMDItemWhereFroms") %>% 
  mutate(url = map(files_small,
                          ~ read_bplist(
                            get_xattr_raw(path = .x,
                                          name = "com.apple.metadata:kMDItemWhereFroms")
                          ) 
  )) %>% 
  select(file, url)

这导致以下dput

df <- structure(list(file = c("'s-Gravenhage_coalitieakkoord.pdf", 
"Aa en Hunze_coalitieakkoord.pdf"), url = list(structure(list(
    plist = structure(list(array = structure(list(string = list(
        "https://denhaag.raadsinformatie.nl/document/6514256/2/RIS299794%20Coalitieakkoord%202018%202022"), 
        string = list()), .Names = c("string", "string"))), .Names = "array", version = "1.0")), .Names = "plist"), 
    structure(list(plist = structure(list(array = structure(list(
        string = list("https://aaenhunze.vvd.nl/uploaded/aaenhunze.vvd.nl/files/5ad8cd44682d8/coalitieakkoord-2018-2022-_definitief_16-april.pdf"), 
        string = list()), .Names = c("string", "string"))), .Names = "array", version = "1.0")), .Names = "plist"))), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -2L), .Names = c("file", 
"url"))


> unnest(df, url)
# A tibble: 2 x 2
  file                              url       
  <chr>                             <list>    
1 's-Gravenhage_coalitieakkoord.pdf <list [1]>
2 Aa en Hunze_coalitieakkoord.pdf   <list [1]>

我想取消列出列表列,但unnest(df, url) 似乎没有完成它的工作。我在这里做错了什么?

【问题讨论】:

    标签: r tidyr purrr


    【解决方案1】:

    你有几个嵌套列表,试试:

    df %>% rowwise %>% mutate(x=unlist(url))
    
    # # A tibble: 2 x 3
    #  file                              url        x                                                                                                                        
    #  <chr>                             <list>     <chr>                                                                                                                    
    #1 's-Gravenhage_coalitieakkoord.pdf <list [1]> https://denhaag.raadsinformatie.nl/document/6514256/2/RIS299794%20Coalitieakkoord%202018%202022                          
    #2 Aa en Hunze_coalitieakkoord.pdf   <list [1]> https://aaenhunze.vvd.nl/uploaded/aaenhunze.vvd.nl/files/5ad8cd44682d8/coalitieakkoord-2018-2022-_definitief_16-april.pdf
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 2021-07-15
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      相关资源
      最近更新 更多