How to do a 2-step wrangling and nesting using tidyr/dplyr, using %>% pipe only?
Solution 1:
Maybe something like this? It first categorizes the data as john
or not, then nests all the data for each category into one list, then pivots those two categories wide.
library(tidyr); library(dplyr)
trb %>%
mutate(column = if_else(name == "john", "dat_john", "other people")) %>%
nest(-column) %>%
pivot_wider(names_from = column, values_from = data)
Solution 2:
It is possible to achieve what you want via a sequence of pipelines. But I am not sure why you want to do this. Note that you need to manually assign "john" as the first level and rearrange the dataframe. Otherwise, if "john" is not the first entry, you won't get him to the leftmost after pivot_wider
.
library(dplyr)
library(tidyr)
trb %>%
group_by(id = factor(name != "john", labels = c("dat_john", "other_people"))) %>%
arrange(id) %>% # use factor and arrange to ensure that john is always the first level
nest(data = -id) %>%
pivot_wider(names_from = id, values_from = data) %>%
mutate(dat_john = with(dat_john[[1L]], list(setNames(dat, type))))
Output
# A tibble: 1 x 2
dat_john other_people
<list> <list>
1 <named list [2]> <tibble [7 x 3]>