首页 > 解决方案 > 使用自定义函数命名由`nest`(tidyr)创建的列表中的项目

问题描述

我有一个我想要的 tibblenest()然后unnest_wider(),同时还以 tibble 格式维护嵌套数据的副本。我知道这听起来不是很优雅,但对于我的用例来说,这是目前最好的解决方案。但是,当我使用该unnest_wider()函数时, name_repair 会创建丑陋...1的 ,...2等名称。如何使用某些purrr功能(https://community.rstudio.com/t/how-to-handle-lack-of-names-with-unnest-wider/ )命名列表中的项目(它们的长度不同) 40496 )? 这样当我unnest_wider()的列有更好的名称时。

我正在寻找的一个小例子:

library(tidyverse)
mpg %>%
  select(manufacturer, model, cty   ) %>%
  group_by(manufacturer, model) %>%
  nest() %>%
  mutate(vars_in_tibble = data) %>%
  ungroup() %>%
  unnest_wider(data) %>%
  unnest_wider(cty)

# A tibble: 38 x 14
   manufacturer model               ...1  ...2  ...3  ...4  ...5  ...6  ...7  ...8  ...9 ...10 ...11 vars_in_tibble       
   <chr>        <chr>              <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <list>               
 1 audi         a4                    18    21    20    21    16    18    18    NA    NA    NA    NA <tibble[,1] [7 x 1]> 
 2 audi         a4 quattro            18    16    20    19    15    17    17    15    NA    NA    NA <tibble[,1] [8 x 1]> 
 3 audi         a6 quattro            15    17    16    NA    NA    NA    NA    NA    NA    NA    NA <tibble[,1] [3 x 1]> 
 4 chevrolet    c1500 suburban 2wd    14    11    14    13    12    NA    NA    NA    NA    NA    NA <tibble[,1] [5 x 1]> 
 5 chevrolet    corvette              16    15    16    15    15    NA    NA    NA    NA    NA    NA <tibble[,1] [5 x 1]> 
 6 chevrolet    k1500 tahoe 4wd       14    11    11    14    NA    NA    NA    NA    NA    NA    NA <tibble[,1] [4 x 1]> 
 7 chevrolet    malibu                19    22    18    18    17    NA    NA    NA    NA    NA    NA <tibble[,1] [5 x 1]> 
 8 dodge        caravan 2wd           18    17    16    16    17    17    11    15    15    16    16 <tibble[,1] [11 x 1]>
 9 dodge        dakota pickup 4wd     15    14    13    14    14    14     9    11    11    NA    NA <tibble[,1] [9 x 1]> 
10 dodge        durango 4wd           13    13     9    13    11    13    11    NA    NA    NA    NA <tibble[,1] [7 x 1]> 

但我想要

# A tibble: 38 x 14
   manufacturer model              car_1 car_2 car_3 car_4 car_5 car_6 car_7 car_8 car_9 car_10 car_11 vars_in_tibble       
   <chr>        <chr>              <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <list>               
 1 audi         a4                    18    21    20    21    16    18    18    NA    NA    NA    NA <tibble[,1] [7 x 1]> 
 2 audi         a4 quattro            18    16    20    19    15    17    17    15    NA    NA    NA <tibble[,1] [8 x 1]> 
 3 audi         a6 quattro            15    17    16    NA    NA    NA    NA    NA    NA    NA    NA <tibble[,1] [3 x 1]> 
 4 chevrolet    c1500 suburban 2wd    14    11    14    13    12    NA    NA    NA    NA    NA    NA <tibble[,1] [5 x 1]> 
 5 chevrolet    corvette              16    15    16    15    15    NA    NA    NA    NA    NA    NA <tibble[,1] [5 x 1]> 
 6 chevrolet    k1500 tahoe 4wd       14    11    11    14    NA    NA    NA    NA    NA    NA    NA <tibble[,1] [4 x 1]> 
 7 chevrolet    malibu                19    22    18    18    17    NA    NA    NA    NA    NA    NA <tibble[,1] [5 x 1]> 
 8 dodge        caravan 2wd           18    17    16    16    17    17    11    15    15    16    16 <tibble[,1] [11 x 1]>
 9 dodge        dakota pickup 4wd     15    14    13    14    14    14     9    11    11    NA    NA <tibble[,1] [9 x 1]> 
10 dodge        durango 4wd           13    13     9    13    11    13    11    NA    NA    NA    NA <tibble[,1] [7 x 1]> 

标签: rdplyrtidyversetidyrpurrr

解决方案


您可以像这样使用 purrr::map 将嵌套列表转换为命名列表

mpg %>%
  select(manufacturer, model, cty) %>%
  group_by(manufacturer, model) %>%
  nest() %>%
  mutate(vars_in_tibble = data,
         data = map(data, ~.x %>% mutate(id = paste0('cty_', row_number())) %>%
                      pivot_wider(names_from = id, values_from = cty)
                    )) %>%
  ungroup() %>%
  unnest(data)

# A tibble: 38 x 14
   manufacturer model              cty_1 cty_2 cty_3 cty_4 cty_5 cty_6 cty_7 cty_8 cty_9 cty_10 cty_11 vars_in_tibble      
   <chr>        <chr>              <int> <int> <int> <int> <int> <int> <int> <int> <int>  <int>  <int> <list>              
 1 audi         a4                    18    21    20    21    16    18    18    NA    NA     NA     NA <tibble[,1] [7 x 1]>
 2 audi         a4 quattro            18    16    20    19    15    17    17    15    NA     NA     NA <tibble[,1] [8 x 1]>
 3 audi         a6 quattro            15    17    16    NA    NA    NA    NA    NA    NA     NA     NA <tibble[,1] [3 x 1]>
 4 chevrolet    c1500 suburban 2wd    14    11    14    13    12    NA    NA    NA    NA     NA     NA <tibble[,1] [5 x 1]>
 5 chevrolet    corvette              16    15    16    15    15    NA    NA    NA    NA     NA     NA <tibble[,1] [5 x 1]>
 6 chevrolet    k1500 tahoe 4wd       14    11    11    14    NA    NA    NA    NA    NA     NA     NA <tibble[,1] [4 x 1]>
 7 chevrolet    malibu                19    22    18    18    17    NA    NA    NA    NA     NA     NA <tibble[,1] [5 x 1]>
 8 dodge        caravan 2wd           18    17    16    16    17    17    11    15    15     16     16 <tibble[,1] [11 x 1~
 9 dodge        dakota pickup 4wd     15    14    13    14    14    14     9    11    11     NA     NA <tibble[,1] [9 x 1]>
10 dodge        durango 4wd           13    13     9    13    11    13    11    NA    NA     NA     NA <tibble[,1] [7 x 1]>
# ... with 28 more rows

推荐阅读