I'm trying to re-format a list containing multiple dataframes into one data frame. I've read around and can't find the specific synthax I need to achieve this.
I have a list:
list1 = list(df1 = data.frame(bread = c("sourdough","baguette","boule","multigrain"), cheese = c("parmigiano","cheddar","mozzarella","stilton")),
df2 = data.frame(bread = c("toast","brioche","focaccia","whole wheat"), cheese = c("gorgonzola","camembert","gouda","feta")))
and I require the dataframe to be stacked vertically with an additional column representing the list element name from which they came, as in the following example:
df = data.frame(breads = c("sourdough","baguette","boule","multigrain","toast","brioche","focaccia","whole wheat"),
cheese = c("parmigiano","cheddar","mozzarella","stilton","gorgonzola","camembert","gouda","feta"),
factor = rep(c("df1","df2"),each = 4))
Very simple, but can't get my head around it.