在R中,如何从分组数据框中创建命名列表?

时间:2017-09-11 03:55:13

标签: r dplyr tidyverse purrr

有几次,我需要将分组数据框的两列转换为一个列表,其中分组列成为列表的名称,而未分组列的值成为元素。

例如,给定starwars数据集,假设我们想要通过他们的家园列出字符。

library(tidyverse)

starwars %>%
  select(homeworld, name) %>%
  group_by(homeworld)

#> # A tibble: 87 x 2
#> # Groups:   homeworld [49]
#>    homeworld               name
#>        <chr>              <chr>
#>  1  Tatooine     Luke Skywalker
#>  2  Tatooine              C-3PO
#>  3     Naboo              R2-D2
#>  4  Tatooine        Darth Vader
#>  5  Alderaan        Leia Organa
#>  6  Tatooine          Owen Lars
#>  7  Tatooine Beru Whitesun lars
#>  8  Tatooine              R5-D4
#>  9  Tatooine  Biggs Darklighter
#> 10   Stewjon     Obi-Wan Kenobi
#> # ... with 77 more rows

如何将此转换为名称为homeworld的列表?也就是说,对于上面的输出,我们得到:

#> $Alderaan
#> [1] "Leia Organa"
#> 
#> $Naboo
#> [1] "R2-D2"
#> 
#> $Stewjon
#> [1] "Obi-Wan Kenobi"
#> 
#> $Tatooine
#> [1] "Luke Skywalker"     "C-3PO"              "Darth Vader"       
#> [4] "Owen Lars"          "Beru Whitesun lars" "R5-D4"             
#> [7] "Biggs Darklighter"

1 个答案:

答案 0 :(得分:1)

归功于@thelatemail,请使用split:

split(df$name,df$homeworld)

输出:

$Alderaan
[1] "Leia Organa"

$Naboo
[1] "R2-D2"

$Stewjon
[1] "Obi-Wan Kenobi"

$Tatooine
[1] "Luke Skywalker"     "C-3PO"              "Darth Vader"       
[4] "Owen Lars"          "Beru Whitesun lars" "R5-D4"             
[7] "Biggs Darklighter" 

示例数据:

require(data.table)
df <- fread("homeworld,               name
              Tatooine,     Luke Skywalker
              Tatooine,              C-3PO
                 Naboo,              R2-D2
              Tatooine,        Darth Vader
              Alderaan,        Leia Organa
              Tatooine,          Owen Lars
              Tatooine, Beru Whitesun lars
              Tatooine,              R5-D4
              Tatooine,  Biggs Darklighter
               Stewjon,     Obi-Wan Kenobi")