25

I have the following process which uses group_split of dplyr:

library(tidyverse)
set.seed(1)
iris %>% sample_n(size = 5) %>% 
    group_by(Species) %>% 
    group_split()

The result is:

[[1]]
# A tibble: 2 x 5
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
         <dbl>       <dbl>        <dbl>       <dbl> <fct>  
1          5           3.5          1.6         0.6 setosa 
2          5.1         3.8          1.5         0.3 setosa 

[[2]]
# A tibble: 2 x 5
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
         <dbl>       <dbl>        <dbl>       <dbl> <fct>     
1          5.9         3            4.2         1.5 versicolor
2          6.2         2.2          4.5         1.5 versicolor

[[3]]
# A tibble: 1 x 5
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species  
         <dbl>       <dbl>        <dbl>       <dbl> <fct>    
1          6.2         3.4          5.4         2.3 virginica

What I want to achieve is to name this list by grouped name (i.e. Species). Yielding this (done by hand):

$setosa
# A tibble: 2 x 5
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
         <dbl>       <dbl>        <dbl>       <dbl> <fct>  
1          5           3.5          1.6         0.6 setosa 
2          5.1         3.8          1.5         0.3 setosa 

$versicolor
# A tibble: 2 x 5
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
         <dbl>       <dbl>        <dbl>       <dbl> <fct>     
1          5.9         3            4.2         1.5 versicolor
2          6.2         2.2          4.5         1.5 versicolor

$virginica
# A tibble: 1 x 5
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species  
         <dbl>       <dbl>        <dbl>       <dbl> <fct>    
1          6.2         3.4          5.4         2.3 virginica

How can I achieve that?

Update

I tried this new data, where the naming now is called Cluster :

df <- structure(list(Cluster = c("Cluster9", "Cluster11", "Cluster1", 
"Cluster9", "Cluster6", "Cluster12", "Cluster9", "Cluster11", 
"Cluster8", "Cluster8"), gene_name = c("Tbc1d8", "Vimp", "Grhpr", 
"H1f0", "Zfp398", "Pikfyve", "Ankrd13a", "Fgfr1op2", "Golga7", 
"Lars2"), p_value = c(3.46629097620496e-47, 3.16837338947245e-62, 
1.55108439059684e-06, 9.46078511685542e-131, 0.000354049720507017, 
0.0146807415917158, 1.42799750295289e-38, 2.0697825959399e-08, 
4.13777221466668e-06, 3.92889640704683e-184), morans_test_statistic = c(14.3797687352223, 
16.6057085487911, 4.66393667525872, 24.301453902967, 3.38642377758137, 
2.17859882998961, 12.9350063459509, 5.48479186018979, 4.4579286289179, 
28.9144540271157), morans_I = c(0.0814728893885783, 0.0947505609609695, 
0.0260671534007409, 0.138921824574569, 0.018764800166045, 0.0119813199210325, 
0.0736554862590782, 0.0309849638728409, 0.0250591347318986, 0.165310420808725
), q_value = c(1.57917584337356e-46, 1.62106594498462e-61, 3.43312171446844e-06, 
6.99503520654745e-130, 0.000683559649593623, 0.0245476826213791, 
5.96116678335584e-38, 4.97603701391971e-08, 8.9649490080526e-06, 
3.48152096326702e-183)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

With Ronak Shah's approach I get inconsistent result:

df %>% group_split(Cluster) %>% setNames(unique(df$Cluster))
$Cluster9
# A tibble: 1 x 6
  Cluster  gene_name    p_value morans_test_statistic morans_I    q_value
  <chr>    <chr>          <dbl>                 <dbl>    <dbl>      <dbl>
1 Cluster1 Grhpr     0.00000155                  4.66   0.0261 0.00000343

$Cluster11
# A tibble: 2 x 6
  Cluster   gene_name  p_value morans_test_statistic morans_I  q_value
  <chr>     <chr>        <dbl>                 <dbl>    <dbl>    <dbl>
1 Cluster11 Vimp      3.17e-62                 16.6    0.0948 1.62e-61
2 Cluster11 Fgfr1op2  2.07e- 8                  5.48   0.0310 4.98e- 8

$Cluster1
# A tibble: 1 x 6
  Cluster   gene_name p_value morans_test_statistic morans_I q_value
  <chr>     <chr>       <dbl>                 <dbl>    <dbl>   <dbl>
1 Cluster12 Pikfyve    0.0147                  2.18   0.0120  0.0245

$Cluster6
# A tibble: 1 x 6
  Cluster  gene_name  p_value morans_test_statistic morans_I  q_value
  <chr>    <chr>        <dbl>                 <dbl>    <dbl>    <dbl>
1 Cluster6 Zfp398    0.000354                  3.39   0.0188 0.000684

$Cluster12
# A tibble: 2 x 6
  Cluster  gene_name   p_value morans_test_statistic morans_I   q_value
  <chr>    <chr>         <dbl>                 <dbl>    <dbl>     <dbl>
1 Cluster8 Golga7    4.14e-  6                  4.46   0.0251 8.96e-  6
2 Cluster8 Lars2     3.93e-184                 28.9    0.165  3.48e-183

$Cluster8
# A tibble: 3 x 6
  Cluster  gene_name   p_value morans_test_statistic morans_I   q_value
  <chr>    <chr>         <dbl>                 <dbl>    <dbl>     <dbl>
1 Cluster9 Tbc1d8    3.47e- 47                  14.4   0.0815 1.58e- 46
2 Cluster9 H1f0      9.46e-131                  24.3   0.139  7.00e-130
3 Cluster9 Ankrd13a  1.43e- 38                  12.9   0.0737 5.96e- 38

Note that $Cluster9 has Cluster1 in it.

Please advice how to go about this?

0

9 Answers 9

23

Lots of good answers. You can also just do:

iris %>% sample_n(size = 5) %>% 
  split(f = as.factor(.$Species))

Which will give you:

$setosa
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
4          5.5         3.5          1.3         0.2  setosa
5          5.3         3.7          1.5         0.2  setosa

$versicolor
  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
3            5         2.3          3.3           1 versicolor

$virginica
  Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
1          7.7         2.6          6.9         2.3 virginica
2          7.2         3.0          5.8         1.6 virginica

Also works with your dataframe above:

df %>% 
  split(f = as.factor(.$Cluster))

Gives you:

$Cluster1
# A tibble: 1 x 6
  Cluster  gene_name    p_value morans_test_statistic morans_I    q_value
  <chr>    <chr>          <dbl>                 <dbl>    <dbl>      <dbl>
1 Cluster1 Grhpr     0.00000155                  4.66   0.0261 0.00000343

$Cluster11
# A tibble: 2 x 6
  Cluster   gene_name  p_value morans_test_statistic morans_I  q_value
  <chr>     <chr>        <dbl>                 <dbl>    <dbl>    <dbl>
1 Cluster11 Vimp      3.17e-62                 16.6    0.0948 1.62e-61
2 Cluster11 Fgfr1op2  2.07e- 8                  5.48   0.0310 4.98e- 8

$Cluster12
# A tibble: 1 x 6
  Cluster   gene_name p_value morans_test_statistic morans_I q_value
  <chr>     <chr>       <dbl>                 <dbl>    <dbl>   <dbl>
1 Cluster12 Pikfyve    0.0147                  2.18   0.0120  0.0245

$Cluster6
# A tibble: 1 x 6
  Cluster  gene_name  p_value morans_test_statistic morans_I  q_value
  <chr>    <chr>        <dbl>                 <dbl>    <dbl>    <dbl>
1 Cluster6 Zfp398    0.000354                  3.39   0.0188 0.000684

$Cluster8
# A tibble: 2 x 6
  Cluster  gene_name   p_value morans_test_statistic morans_I   q_value
  <chr>    <chr>         <dbl>                 <dbl>    <dbl>     <dbl>
1 Cluster8 Golga7    4.14e-  6                  4.46   0.0251 8.96e-  6
2 Cluster8 Lars2     3.93e-184                 28.9    0.165  3.48e-183

$Cluster9
# A tibble: 3 x 6
  Cluster  gene_name   p_value morans_test_statistic morans_I   q_value
  <chr>    <chr>         <dbl>                 <dbl>    <dbl>     <dbl>
1 Cluster9 Tbc1d8    3.47e- 47                  14.4   0.0815 1.58e- 46
2 Cluster9 H1f0      9.46e-131                  24.3   0.139  7.00e-130
3 Cluster9 Ankrd13a  1.43e- 38                  12.9   0.0737 5.96e- 38
20

Not sure, if this can be done directly. One way is by sampling the dataframe and then use it's unique names to setNames.

library(dplyr)

df <- iris %>% sample_n(size = 5) 

df %>%
   group_split(Species) %>%
   setNames(unique(df$Species))


#$setosa
# A tibble: 1 x 5
#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#         <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#1            5         3.4          1.5         0.2 setosa 

#$versicolor
# A tibble: 1 x 5
#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
#         <dbl>       <dbl>        <dbl>       <dbl> <fct>     
#1            6         3.4          4.5         1.6 versicolor

#$virginica
# A tibble: 3 x 5
#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species  
#         <dbl>       <dbl>        <dbl>       <dbl> <fct>    
#1          7.3         2.9          6.3         1.8 virginica
#2          6.9         3.1          5.1         2.3 virginica
#3          7.7         3            6.1         2.3 virginica

It is weird that group_split doesn't directly name the lists because it is supposed to be an alternative to base::split which does name it.

split(df, df$Species)

The document says :

group_split() works like base::split() but

  • it uses the grouping structure from group_by() and therefore is subject to the data mask
  • it does not name the elements of the list based on the grouping as this typically loses information and is confusing.

For the updated dataset it doesn't work because while naming we are using unique which gets the data in the same order as they appear whereas group_split, splits the data based on increasing order of their value. (So the order of splitting is Cluster1,Cluster11, Cluster2...) One way to overcome that is to convert Cluster to factor and specify levels as they appear using unique.

df <- df %>%
      mutate(Cluster = factor(Cluster, levels = unique(Cluster))) 

df %>%
   group_split(Cluster) %>%
   setNames(unique(df$Cluster))

OR if you don't want them as factors do

df %>%
  group_split(Cluster) %>%
  setNames(sort(unique(df$Cluster)))
4
  • Thanks. But I tried with my case (see update). The naming is inconsistent with the desired column. Commented Jul 19, 2019 at 7:51
  • Is there a way to achieve this without invoking the original dataframe? For example, when you used mutate, you saved the mutated dataframe in an object. But if we have a long series of pipes and we want to avoid saving the resulting object, but still name the tables by some variable which is not in the original dataframe, but was created by pipes, would this be possible?
    – J. Doe
    Commented Aug 12, 2020 at 14:12
  • 1
    Depends on your case but I think it should be possible. If the variable is created in the pipes, you can use . to refer the object. You can use .$column_name to refer a column created previously.
    – Ronak Shah
    Commented Aug 12, 2020 at 14:50
  • 1
    From dplyr developer: "We strongly believe giving it names is an anti pattern, hence that's not happening in dplyr, but we do provide some tools you can use to make your own versions with names." github.com/tidyverse/dplyr/issues/4223
    – Tung
    Commented Apr 15, 2021 at 17:29
14

The solution below

  • Relies on tidyverse tools
  • Fits into a linear pipeline
  • Does not rely on the order of factors or vectors, can be applied to any list of data frames

It is almost the same as Kim's solution, however the more appropriate choice of functions makes it better readable and simpler.

library(dplyr)
library(purrr)

iris %>%
dplyr::group_split(Species) %>%
purrr::set_names(purrr::map_chr(., ~.x$Species[1]))
2
  • 3
    Late to this issue, but this is a smart, elegant solution, imo.
    – rdelrossi
    Commented Apr 23, 2022 at 3:48
  • 2
    the solution as given is giving names "1" "2" "3", evidently because map_chr is interpreting the factor levels as integers instead of strings. I can't find another way to get it to work without coercing Species into character class prior to the split
    – flies
    Commented Feb 7, 2023 at 21:00
9

If you want to split the data frame by more than one group and have the named list, the tidytable package has a group_split.() function for that.

### pacman will check and install missing packages if needed
if (!require("pacman")) install.packages("pacman")
pacman::p_load(gapminder)
pacman::p_load(tidytable)

Split by one group. Retain the group in the data frame by using the option .keep

gapminder_split_1group <- gapminder %>% 
  group_split.(continent, .keep = FALSE, .named = TRUE)
gapminder_split_1group
#> $Asia
#> # A tidytable: 396 x 5
#>    country      year lifeExp      pop gdpPercap
#>    <fct>       <int>   <dbl>    <int>     <dbl>
#>  1 Afghanistan  1952    28.8  8425333      779.
#>  2 Afghanistan  1957    30.3  9240934      821.
#>  3 Afghanistan  1962    32.0 10267083      853.
#>  4 Afghanistan  1967    34.0 11537966      836.
#>  5 Afghanistan  1972    36.1 13079460      740.
#>  6 Afghanistan  1977    38.4 14880372      786.
#>  7 Afghanistan  1982    39.9 12881816      978.
#>  8 Afghanistan  1987    40.8 13867957      852.
#>  9 Afghanistan  1992    41.7 16317921      649.
#> 10 Afghanistan  1997    41.8 22227415      635.
#> # ... with 386 more rows
#> 
#> $Europe
#> # A tidytable: 360 x 5
#>    country  year lifeExp     pop gdpPercap
#>    <fct>   <int>   <dbl>   <int>     <dbl>
#>  1 Albania  1952    55.2 1282697     1601.
#>  2 Albania  1957    59.3 1476505     1942.
#>  3 Albania  1962    64.8 1728137     2313.
#>  4 Albania  1967    66.2 1984060     2760.
#>  5 Albania  1972    67.7 2263554     3313.
#>  6 Albania  1977    68.9 2509048     3533.
#>  7 Albania  1982    70.4 2780097     3631.
#>  8 Albania  1987    72   3075321     3739.
#>  9 Albania  1992    71.6 3326498     2497.
#> 10 Albania  1997    73.0 3428038     3193.
#> # ... with 350 more rows
#> 
#> $Africa
#> # A tidytable: 624 x 5
#>    country  year lifeExp      pop gdpPercap
#>    <fct>   <int>   <dbl>    <int>     <dbl>
#>  1 Algeria  1952    43.1  9279525     2449.
#>  2 Algeria  1957    45.7 10270856     3014.
#>  3 Algeria  1962    48.3 11000948     2551.
#>  4 Algeria  1967    51.4 12760499     3247.
#>  5 Algeria  1972    54.5 14760787     4183.
#>  6 Algeria  1977    58.0 17152804     4910.
#>  7 Algeria  1982    61.4 20033753     5745.
#>  8 Algeria  1987    65.8 23254956     5681.
#>  9 Algeria  1992    67.7 26298373     5023.
#> 10 Algeria  1997    69.2 29072015     4797.
#> # ... with 614 more rows
#> 
#> $Americas
#> # A tidytable: 300 x 5
#>    country    year lifeExp      pop gdpPercap
#>    <fct>     <int>   <dbl>    <int>     <dbl>
#>  1 Argentina  1952    62.5 17876956     5911.
#>  2 Argentina  1957    64.4 19610538     6857.
#>  3 Argentina  1962    65.1 21283783     7133.
#>  4 Argentina  1967    65.6 22934225     8053.
#>  5 Argentina  1972    67.1 24779799     9443.
#>  6 Argentina  1977    68.5 26983828    10079.
#>  7 Argentina  1982    69.9 29341374     8998.
#>  8 Argentina  1987    70.8 31620918     9140.
#>  9 Argentina  1992    71.9 33958947     9308.
#> 10 Argentina  1997    73.3 36203463    10967.
#> # ... with 290 more rows
#> 
#> $Oceania
#> # A tidytable: 24 x 5
#>    country    year lifeExp      pop gdpPercap
#>    <fct>     <int>   <dbl>    <int>     <dbl>
#>  1 Australia  1952    69.1  8691212    10040.
#>  2 Australia  1957    70.3  9712569    10950.
#>  3 Australia  1962    70.9 10794968    12217.
#>  4 Australia  1967    71.1 11872264    14526.
#>  5 Australia  1972    71.9 13177000    16789.
#>  6 Australia  1977    73.5 14074100    18334.
#>  7 Australia  1982    74.7 15184200    19477.
#>  8 Australia  1987    76.3 16257249    21889.
#>  9 Australia  1992    77.6 17481977    23425.
#> 10 Australia  1997    78.8 18565243    26998.
#> # ... with 14 more rows

Split by 2 groups

gapminder_split_2group <- gapminder %>% 
  group_split.(continent, country, .keep = FALSE, .named = TRUE)
head(gapminder_split_2group)
#> $Asia.Afghanistan
#> # A tidytable: 12 x 4
#>     year lifeExp      pop gdpPercap
#>    <int>   <dbl>    <int>     <dbl>
#>  1  1952    28.8  8425333      779.
#>  2  1957    30.3  9240934      821.
#>  3  1962    32.0 10267083      853.
#>  4  1967    34.0 11537966      836.
#>  5  1972    36.1 13079460      740.
#>  6  1977    38.4 14880372      786.
#>  7  1982    39.9 12881816      978.
#>  8  1987    40.8 13867957      852.
#>  9  1992    41.7 16317921      649.
#> 10  1997    41.8 22227415      635.
#> 11  2002    42.1 25268405      727.
#> 12  2007    43.8 31889923      975.
#> 
#> $Europe.Albania
#> # A tidytable: 12 x 4
#>     year lifeExp     pop gdpPercap
#>    <int>   <dbl>   <int>     <dbl>
#>  1  1952    55.2 1282697     1601.
#>  2  1957    59.3 1476505     1942.
#>  3  1962    64.8 1728137     2313.
#>  4  1967    66.2 1984060     2760.
#>  5  1972    67.7 2263554     3313.
#>  6  1977    68.9 2509048     3533.
#>  7  1982    70.4 2780097     3631.
#>  8  1987    72   3075321     3739.
#>  9  1992    71.6 3326498     2497.
#> 10  1997    73.0 3428038     3193.
#> 11  2002    75.7 3508512     4604.
#> 12  2007    76.4 3600523     5937.
#> 
#> $Africa.Algeria
#> # A tidytable: 12 x 4
#>     year lifeExp      pop gdpPercap
#>    <int>   <dbl>    <int>     <dbl>
#>  1  1952    43.1  9279525     2449.
#>  2  1957    45.7 10270856     3014.
#>  3  1962    48.3 11000948     2551.
#>  4  1967    51.4 12760499     3247.
#>  5  1972    54.5 14760787     4183.
#>  6  1977    58.0 17152804     4910.
#>  7  1982    61.4 20033753     5745.
#>  8  1987    65.8 23254956     5681.
#>  9  1992    67.7 26298373     5023.
#> 10  1997    69.2 29072015     4797.
#> 11  2002    71.0 31287142     5288.
#> 12  2007    72.3 33333216     6223.
#> 
#> $Africa.Angola
#> # A tidytable: 12 x 4
#>     year lifeExp      pop gdpPercap
#>    <int>   <dbl>    <int>     <dbl>
#>  1  1952    30.0  4232095     3521.
#>  2  1957    32.0  4561361     3828.
#>  3  1962    34    4826015     4269.
#>  4  1967    36.0  5247469     5523.
#>  5  1972    37.9  5894858     5473.
#>  6  1977    39.5  6162675     3009.
#>  7  1982    39.9  7016384     2757.
#>  8  1987    39.9  7874230     2430.
#>  9  1992    40.6  8735988     2628.
#> 10  1997    41.0  9875024     2277.
#> 11  2002    41.0 10866106     2773.
#> 12  2007    42.7 12420476     4797.
#> 
#> $Americas.Argentina
#> # A tidytable: 12 x 4
#>     year lifeExp      pop gdpPercap
#>    <int>   <dbl>    <int>     <dbl>
#>  1  1952    62.5 17876956     5911.
#>  2  1957    64.4 19610538     6857.
#>  3  1962    65.1 21283783     7133.
#>  4  1967    65.6 22934225     8053.
#>  5  1972    67.1 24779799     9443.
#>  6  1977    68.5 26983828    10079.
#>  7  1982    69.9 29341374     8998.
#>  8  1987    70.8 31620918     9140.
#>  9  1992    71.9 33958947     9308.
#> 10  1997    73.3 36203463    10967.
#> 11  2002    74.3 38331121     8798.
#> 12  2007    75.3 40301927    12779.
#> 
#> $Oceania.Australia
#> # A tidytable: 12 x 4
#>     year lifeExp      pop gdpPercap
#>    <int>   <dbl>    <int>     <dbl>
#>  1  1952    69.1  8691212    10040.
#>  2  1957    70.3  9712569    10950.
#>  3  1962    70.9 10794968    12217.
#>  4  1967    71.1 11872264    14526.
#>  5  1972    71.9 13177000    16789.
#>  6  1977    73.5 14074100    18334.
#>  7  1982    74.7 15184200    19477.
#>  8  1987    76.3 16257249    21889.
#>  9  1992    77.6 17481977    23425.
#> 10  1997    78.8 18565243    26998.
#> 11  2002    80.4 19546792    30688.
#> 12  2007    81.2 20434176    34435.

Created on 2021-04-15 by the reprex package (v2.0.0)

8

I came across same problem and using this 2 step solution:

df= df %>% group_by(Cluster)
df= df %>% group_split() %>% set_names(unlist(group_keys(df)))     
df$Cluster1
# A tibble: 1 x 6
  Cluster  gene_name    p_value morans_test_statistic morans_I    q_value
  <chr>    <chr>          <dbl>                 <dbl>    <dbl>      <dbl>
1 Cluster1 Grhpr     0.00000155                  4.66   0.0261 0.00000343
df$Cluster9
# A tibble: 3 x 6
  Cluster  gene_name   p_value morans_test_statistic morans_I   q_value
  <chr>    <chr>         <dbl>                 <dbl>    <dbl>     <dbl>
1 Cluster9 Tbc1d8    3.47e- 47                  14.4   0.0815 1.58e- 46
2 Cluster9 H1f0      9.46e-131                  24.3   0.139  7.00e-130
3 Cluster9 Ankrd13a  1.43e- 38                  12.9   0.0737 5.96e- 38
1
  • 2
    If using more than one column to group, you need interaction(group_keys(df)) instead of unlist() Commented Nov 9, 2021 at 20:32
4

The developers have made it clear that they are not interested in providing an option to return a named list. Again, I'd like to second a feature request but the old issue has been locked here.

A hack that I came up with is simply taking the assignment operator within the pipe:

library(tidyverse)
iris %>% 
  sample_n(size = 5) %>% 
  group_split(Species, .keep = TRUE) %>%
  `names<-`({.} %>% map(~ .x$Species[1]) %>% unlist()) %>%
  ## If you want to discard the grouping variable, do the following step as well
  map(~ .x %>% select(-Species))

Not an intuitive answer to remember, but this will keep it neatly within a pipe.

2

An optional add-on solution to get rid of the extra column:

iris %>% sample_n(size = 5) %>%
split(.$Species) %>%
map(~select(., -Species))
0

Use a for loop that accesses unique elements of Cluster in each df and then assigns them as respective names.

x.names <-NULL
for (i in 1:length(df)){
  x.names[i]<-c(unique(df[[i]]$Cluster))
  names(df)<-x.names
}
0

Another potential fixed would be

library(tidyverse)
list_cyl <- mtcars %>% 
   group_by(cyl) %>%
   # apply any functions to the grouped data 
   # to create a list column where each row is the grouped data
   # here are some examples
   do(# res = select_all(.),
      res = as_tibble(.)) %>%
   ungroup() %>%
   # set name of each grouped data
   mutate(res = set_names(res, paste0("Cylinder: ", cyl))) %>%
   # extract the list column
   pull(res)
   
list_cyl
$`Cylinder: 4`
# A tibble: 11 × 11
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  22.8     4 108      93  3.85  2.32  18.6     1     1     4     1
 2  24.4     4 147.     62  3.69  3.19  20       1     0     4     2
 3  22.8     4 141.     95  3.92  3.15  22.9     1     0     4     2
 4  32.4     4  78.7    66  4.08  2.2   19.5     1     1     4     1
 5  30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2
 6  33.9     4  71.1    65  4.22  1.84  19.9     1     1     4     1
 7  21.5     4 120.     97  3.7   2.46  20.0     1     0     3     1
 8  27.3     4  79      66  4.08  1.94  18.9     1     1     4     1
 9  26       4 120.     91  4.43  2.14  16.7     0     1     5     2
10  30.4     4  95.1   113  3.77  1.51  16.9     1     1     5     2
11  21.4     4 121     109  4.11  2.78  18.6     1     1     4     2

$`Cylinder: 6`
# A tibble: 7 × 11
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
3  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
4  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
5  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
6  17.8     6  168.   123  3.92  3.44  18.9     1     0     4     4
7  19.7     6  145    175  3.62  2.77  15.5     0     1     5     6

$`Cylinder: 8`
# A tibble: 14 × 11
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
 2  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
 3  16.4     8  276.   180  3.07  4.07  17.4     0     0     3     3
 4  17.3     8  276.   180  3.07  3.73  17.6     0     0     3     3
 5  15.2     8  276.   180  3.07  3.78  18       0     0     3     3
 6  10.4     8  472    205  2.93  5.25  18.0     0     0     3     4
 7  10.4     8  460    215  3     5.42  17.8     0     0     3     4
 8  14.7     8  440    230  3.23  5.34  17.4     0     0     3     4
 9  15.5     8  318    150  2.76  3.52  16.9     0     0     3     2
10  15.2     8  304    150  3.15  3.44  17.3     0     0     3     2
11  13.3     8  350    245  3.73  3.84  15.4     0     0     3     4
12  19.2     8  400    175  3.08  3.84  17.0     0     0     3     2
13  15.8     8  351    264  4.22  3.17  14.5     0     1     5     4
14  15       8  301    335  3.54  3.57  14.6     0     1     5     8

I prefer this approach over the group_split() + setNames(unique(*grouping column*)) approach because it doesn't require the names to be obtained from an existing column of the original data set (i.e. mtcars in my example)

As an example, suppose I want to create a list of the mtcars dataset by split by the number of cylinder and transmission type.

Using the `group_split()` + `setNames(unique(*grouping column*))` approach:
list_cyl_am1 <- mtcars %>%
  # create cylinder + transmission type group identifier
  mutate(am_text = ifelse(am == 1, "Manual", "Automatic"),
         grp_id = paste0("Cylinder: ", cyl, " & ", am_text, " Transmission")) %>%
  group_split(grp_id) %>%
  setNames(unique(mtcars$grp_id))
names(list_cyl_am1)
NULL

But using my approach, it will give me what I need

list_cyl_am2 <- mtcars %>%
  # create cylinder + transmission type group identifier
  mutate(am_text = ifelse(am == 1, "Manual", "Automatic"),
         grp_id = paste0("Cylinder: ", cyl, " & ", am_text, " Transmission")) %>%
  group_by(grp_id) %>%
  # apply some functions to the grouped data
  do(res = as_tibble(.)) %>%
  ungroup() %>%
  # set name
  mutate(res = set_names(res, grp_id)) %>%
  # extract the list column
  pull(res)

names(list_cyl_am2)
"Cylinder: 4 & Automatic Transmission" "Cylinder: 4 & Manual Transmission"   
"Cylinder: 6 & Automatic Transmission" "Cylinder: 6 & Manual Transmission"   
"Cylinder: 8 & Automatic Transmission" "Cylinder: 8 & Manual Transmission"   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.