1
library(ggmosaic)
library(tidyverse)

I'm struggling to split a dataset into multiple tables using Tidyverse methods. I'll use the code below to create a dataset with a somewhat similar structure as my actual data.

happy2<-happy%>%
select(sex,marital,degree,health)%>%
group_by(sex,marital,degree,health)%>%
summarise(Count=n())

Now, using the happy2 dataset, I would like to split the data by "degree", and within each category of degree, there will be two tables, one for male and one for female, based on the "sex" variable. Each table will have "marital" and "Count" as the columns and "health" as the rows.

I'm hoping to find an elegant way to create these tables using Tidyverse methods, such as tidyr::nest, purrr, or split.

Mike
  • 2,017
  • 6
  • 26
  • 53

1 Answers1

0

This seems a fairly straight forward application of split:

# For a flat list
happy2 %>%
  split(list(.$degree, .$sex))

# For a nested list
happy2 %>% 
  split(.$degree) %>% 
  lapply(function(x) split(x, x$sex))

Both approaches are efficient and the syntax is fairly clean and easy to understand; I am not sure why a tidyverse equivalent should be desirable.

Stefan F
  • 2,573
  • 1
  • 17
  • 19
  • I may have been asking too much in the question. I see now how I can use split to achieve the first part, but once I have the lists, the next part is reshaping the tables. I created another question as a second part titled "Split a Dataset into a Nested List of Dataframes and then Spread Using Tidyr and Purrr". This is why I was thinking Tidyverse methods would be most useful, especially purrr, for spreading the tables using Tidyr. – Mike Aug 14 '17 at 01:29