0

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.

Dasr
  • 777
  • 6
  • 16
  • 1
    Does this answer your question? [Convert a list of data frames into one data frame](https://stackoverflow.com/questions/2851327/convert-a-list-of-data-frames-into-one-data-frame) – desval Jul 09 '20 at 13:23

2 Answers2

1

you can use

do.call(rbind,list1)

            bread     cheese
df1.1   sourdough parmigiano
df1.2    baguette    cheddar
df1.3       boule mozzarella
df1.4  multigrain    stilton
df2.1       toast gorgonzola
df2.2     brioche  camembert
df2.3    focaccia      gouda
df2.4 whole wheat       feta

edit:

if you want explicit "From" col

new_df <- do.call(rbind,list1)
new_df$From <- sub("\\..*$","",rownames(new_df))

            bread     cheese From
df1.1   sourdough parmigiano  df1
df1.2    baguette    cheddar  df1
df1.3       boule mozzarella  df1
df1.4  multigrain    stilton  df1
df2.1       toast gorgonzola  df2
df2.2     brioche  camembert  df2
df2.3    focaccia      gouda  df2
df2.4 whole wheat       feta  df2
Daniel O
  • 4,258
  • 6
  • 20
  • mmm....this seems to work with the example I have given.....but not my original dataset. There must me something I'm missing. This is the structure of my original dataset (only first element shown), they are matrix instead of data.frames, I guess this is where it makes a difference? ```List of 11 $ 1 : num [1:1000, 1:3] 0.85 0.849 0.847 0.846 0.844 ... ..- attr(*, "dimnames")=List of 2 .. ..$ : chr [1:1000] "1" "2" "3" "4" ... .. ..$ : chr [1:3] "fit2" "lwr2" "upr2"``` – Dasr Jul 09 '20 at 13:30
  • 1
    Ok I think I got it. Wasn't working because they were matrices I think. I did ```lapply(list1,function(x) as.data.frame(x))``` then used your code and it seems to have worked. – Dasr Jul 09 '20 at 13:33
  • for the exact same results, you can shorten that to `lapply(list1, as.data.frame)` – Daniel O Jul 09 '20 at 13:36
0

You can try this:

#Bind
DF <- do.call(rbind,list1)
#Create factor
DF$factor <- rownames(DF)
rownames(DF)<-NULL
DF$factor <- gsub("\\..*","",DF$factor)
DF

        bread     cheese factor
1   sourdough parmigiano    df1
2    baguette    cheddar    df1
3       boule mozzarella    df1
4  multigrain    stilton    df1
5       toast gorgonzola    df2
6     brioche  camembert    df2
7    focaccia      gouda    df2
8 whole wheat       feta    df2
Duck
  • 39,058
  • 13
  • 42
  • 84