1

My question is basically this question, but I want to achieve it without breaking the pipeline.

In order to achieve this, I tried to follow the advice from the following question. However, it's not working. Here is my code:

iris %>%
group_by(Species) %>%
      {
        {. %>% group_keys() -> tmp}
      } %>% 
      group_split()

Can anyone help point me in the right direction?

Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
J. Doe
  • 1,544
  • 1
  • 11
  • 26
  • The `names<-` part is missing from my code, but my code is already not working before that part comes into play so I don't consider it so relevant (once this chunk of code starts working, it's fairly easy to assign names with it). – J. Doe Sep 03 '20 at 10:20
  • How about `iris %>% group_split(Species) %>% setNames(unique(iris$Species))`. Will that work? Check https://stackoverflow.com/questions/57107721/how-to-name-the-list-of-the-group-split-output-in-dplyr – Ronak Shah Sep 03 '20 at 10:21
  • Note that the solution in Ronak's comment may cause wrong matching. Try `iris$Species <- factor(iris$Species, levels = rev(levels(iris$Species)))` and run his code again, you will see the trap. – Darren Tsai Sep 04 '20 at 06:45

1 Answers1

3

You can include group_keys() in the pipe like this:

library(dplyr)

iris %>%
  group_by(Species) %>%
  {setNames(group_split(.), group_keys(.)[[1]])}

Output

$setosa
# A tibble: 50 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1          5.1         3.5          1.4         0.2 setosa 
 2          4.9         3            1.4         0.2 setosa 
 3          4.7         3.2          1.3         0.2 setosa 
 4          4.6         3.1          1.5         0.2 setosa 
 5          5           3.6          1.4         0.2 setosa 
 6          5.4         3.9          1.7         0.4 setosa 
 7          4.6         3.4          1.4         0.3 setosa 
 8          5           3.4          1.5         0.2 setosa 
 9          4.4         2.9          1.4         0.2 setosa 
10          4.9         3.1          1.5         0.1 setosa 
# … with 40 more rows

$versicolor
# A tibble: 50 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
          <dbl>       <dbl>        <dbl>       <dbl> <fct>     
 1          7           3.2          4.7         1.4 versicolor
 2          6.4         3.2          4.5         1.5 versicolor
 3          6.9         3.1          4.9         1.5 versicolor
 4          5.5         2.3          4           1.3 versicolor
 5          6.5         2.8          4.6         1.5 versicolor
 6          5.7         2.8          4.5         1.3 versicolor
 7          6.3         3.3          4.7         1.6 versicolor
 8          4.9         2.4          3.3         1   versicolor
 9          6.6         2.9          4.6         1.3 versicolor
10          5.2         2.7          3.9         1.4 versicolor
# … with 40 more rows

$virginica
# A tibble: 50 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species  
          <dbl>       <dbl>        <dbl>       <dbl> <fct>    
 1          6.3         3.3          6           2.5 virginica
 2          5.8         2.7          5.1         1.9 virginica
 3          7.1         3            5.9         2.1 virginica
 4          6.3         2.9          5.6         1.8 virginica
 5          6.5         3            5.8         2.2 virginica
 6          7.6         3            6.6         2.1 virginica
 7          4.9         2.5          4.5         1.7 virginica
 8          7.3         2.9          6.3         1.8 virginica
 9          6.7         2.5          5.8         1.8 virginica
10          7.2         3.6          6.1         2.5 virginica
# … with 40 more rows
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51