Seb
Seb

Reputation: 53

Tidyverse: Unnest tibble by group into seperate df/tibble

I am currently trying to find a short & tidy way to unnest a nested tibble with 2 grouping variables and a tibble/df as data for each observation into a tibble having only one of the grouping variables and the respective data in a df (or tibble). I will illustrate my sample by using the starwars dataset provided by tidyverse and show the 3 solutions I came up with so far.

library(tidyverse)

#Set example data: 2 grouping variables, name & sex, and one data column with a tibble/df for each observation
tbl_1 <- starwars %>% group_by(name, sex) %>% nest() %>% ungroup()

#1st Solution: Neither short nor tidy but gets me the result I would like to have in the end
tbl_2 <- tbl_1 %>%
  group_by(sex) %>%
  nest() %>%
  ungroup()%>%
  mutate(data = map(.$data, ~.x %>% group_by(name) %>% unnest(c(data))))

#2nd Solution: A lot shorter and more neat but still not what I have in mind
tbl_2 <- tbl_1 %>%
  nest(-sex) %>%
  mutate(data = map(.$data, ~.x %>% unnest(cols = c(data))))

#3rd Solution: The best so far, short and readable 
tbl_2 <- tbl_1 %>% 
  unnest(data) %>% 
  group_by(name) %>%
  nest(-sex)

##Solution as I have it in mind / I think should be somehow possible.
tbl_2 <- tbl_1 %>% group_by(sex) %>% unnest() #This however gives one large tibble grouped by sex, not two separate tibbles in a nested tibble 

Is such a solution I am looking for even possible in the first place or is the 3rd solution as close as it gets in terms of being both short, readable and tidy?

In terms of my actual workflow tbl_1 is the "work horse" of my analysis and not subject to change, I use to apply analysis or ggplot via map for figures etc., which are sometimes on the level of "names" or "sex".

I appreciate any input!

Update: User @caldwellst has given a sufficient enough answer for me to mark this question as answered, unfortunately only as a comment. After waiting a bit, I would now accept any other answer with the same suggestion as the solution to mark this question as solved.

Upvotes: 1

Views: 345

Answers (1)

Seb
Seb

Reputation: 53

As @caldwellst has pointed out in a comment, the group_by is unnecessary, the provided solution is sufficiently short and tidy enough for me in that case. tbl_1 %>% unnest(data) %>% nest(data = -sex).

I will remove my answer and accept a different one, if @caldwellst posts the comment as answer or somebody else provides a different, but equally suitable one.

Upvotes: 0

Related Questions