Creating a list of data frames and then assigning group names

# data
df <- Titanic %>% as_tibble()
#> # A tibble: 6 x 5
#>   Class Sex    Age   Survived     n
#>   <chr> <chr>  <chr> <chr>    <dbl>
#> 1 1st   Male   Child No           0
#> 2 2nd   Male   Child No           0
#> 3 3rd   Male   Child No          35
#> 4 Crew  Male   Child No           0
#> 5 1st   Female Child No           0
#> 6 2nd   Female Child No           0

How do I create a list of data frames that correspond to only one Class? In the above example, there are four different classes. So the list will contain four data frames. After that, I want to assign names to the list's elements. The names will be corresponding Class names.

Please note that I do not want to manually create the list and assign the names.

Why do you want to do this?
I mean: what are the next steps you want to do with this list of data.frames?

Write an excel file using writexl::write_xlsx(), each sheet will get the names from the list's names.

See e.g.
For the 'next' part you could have a look at the purrr map variants.

1 Like

Thank you! The following does the job!

split(df, f = df$Class)

I believe there are many other ways to achieve this. I hope somebody will share.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.