I'm using the map family in purrr; and I'm having a hard time figuring how encode a named list item into a variable (to use as a grouping var in ggplot) within a dataframe.
In fact, I'm wondering if I'm even thinking about the data structures right: should i be relying on nested dfs instead of a list?
In any case, here's what i'm trying:
library(tidyverse)
# function that returns a df with vars x and y
toy_fn <- function(n) { tibble(x = seq(1,n), y = n*1.1) }
# some inputs for my toy function
my_n <- c(10,15,30,50)
x <- my_n %>% map(.f = toy_fn) %>% set_names(paste0("n_",my_n))
# x is a named list of four dfs that I'd like to plot with ggplot2::facet_grid
# and it's here where I'm stuck
# I need to 1) collapse the list into a single df and 2) capture a unique preserving variable to which to map the facet grid ("df$name")
# I'd expect a a final tibble df with vars x, y, and a created "name" var, which i'd pass to ggplot like so:
#ggplot(df) + geom_line(aes(x,y)) + facet_wrap(~name)
Thank you much; I’d noticed that bind_rows nicely condensed a list of dfs, but I was unaware of the .id argument, which I see is likewise available to many of the map_* like functions.