I need to accomplish a wrangling task with tidyr
/dplyr
as part of a %>%
pipe. That is, without assigning data to helper objects. I have the following trb
tibble as a given:
library(tibble)
trb <-
tribble(~name, ~type, ~dat,
"john", "cat", mtcars,
"john", "spider", Puromycin,
"amanda", "dog", ToothGrowth,
"chris", "wolf", PlantGrowth,
"annie", "lion", women,
"richard", "frog", trees,
"liz", "horse", USArrests,
"raul", "snake", iris,
"kate" , "bear", quakes)
and I want to do a 2-step wrangling (not necessarily in the following order):
- lump together john's
dat
data frames into a named list (in which names will come fromtype
); and - shift john's information to leftmost while nesting the data of the others.
The desired output should therefore be:
desired_output <-
tribble(~dat_john, ~other_people,
list("cat" = mtcars, "spider" = Puromycin), trb %>% dplyr::filter(name != "john")
)
As noted above, it's important to me to get from trb
to desired_output
using %>%
only. Any ideas?