0

Is there any way to split one dataframe into subsets of dataframes with dplyr according to group_by groups?

mtcars %>% group_by(cyl, gear) %>% (codes?)

thank a lot!

zesla
  • 11,155
  • 16
  • 82
  • 147

1 Answers1

4

Well, not that you'd really want to - but you can do it with tidyr, which is nearly the same thing.

library(dplyr)
library(tidyr)

data(iris)

iris %>%
    group_by(Species) %>%
    nest() %>%
    select(data) %>%
    unlist(recursive = F)

#> $data1
#> # A tibble: 50 x 4
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width
#>           <dbl>       <dbl>        <dbl>       <dbl>
#> 1           5.1         3.5          1.4         0.2
#> 2           4.9         3.0          1.4         0.2
#> 
#> $data2
#> # A tibble: 50 x 4
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width
#>           <dbl>       <dbl>        <dbl>       <dbl>
#> 1           7.0         3.2          4.7         1.4
#> 2           6.4         3.2          4.5         1.5
#> 
#> $data3
#> # A tibble: 50 x 4
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width
#>           <dbl>       <dbl>        <dbl>       <dbl>
#> 1           6.3         3.3          6.0         2.5
#> 2           5.8         2.7          5.1         1.9
Michael Griffiths
  • 1,399
  • 7
  • 14