1

I have a dataset with a categorical variable and I would like to split it into many datasets named by that categorical variable. For example, in the Titanic dataset there is an age variable. I would like to have R automatically figure out that it has two levels, Adult and Child, and create a dataset named Adult containing the Adult records and another dataset named Child containing the Child records. How can I do this with tidyverse or base R? I know that I can just filter and name each object manually, but I would like to do this programmatically for a variable with many levels.

The list2env() option worked perfectly. Thank you @gregorthomas. My final code was like this

Titanic <- data.frame(Titanic)

split(Titanic, with(Titanic, Age), drop = TRUE) |> 
  list2env(.GlobalEnv)
wdefreit
  • 51
  • 3
  • 2
    Use the `split` function. `split(Titanic, Titanic$age)`. Or with `dplyr` `Titanic %>% group_by(age) %>% group_split()`. These will both create a `list` of data frames as the result. [Lists of data frames are good](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames), but if you really want a bunch of separate objects you can use `list2env()` to put the individual data frames into your global environment. – Gregor Thomas Sep 29 '22 at 17:33
  • 1
    (But also be sure that you want to do this at all... if you want to do similar things to each little data frame, it's probably easier to do it to the data frames in a list, and even easier to use `dplyr` to do things "by group" in one big data frame.) – Gregor Thomas Sep 29 '22 at 17:37

0 Answers0