Achal Neupane
Achal Neupane

Reputation: 5729

Merge multiple dataframes while also adding a new column with the corresponding dataframe name

I have a list of dataframes

my_list <- list(structure(c("23000 Vs 23500", "23500 Vs 24000", "1.03546847852537", 
"0.735744771309744", "15", "29"), .Dim = 2:3, .Dimnames = list(
    NULL, c("Group", "EffectSize", "RequiredReplicates"))), structure(c("23500 Vs 24000", 
"24000 Vs 25000", "25000 Vs 25500", "0.735744771309744", "1.48620682621918", 
"0.418877850096638", "29", "7", "89"), .Dim = c(3L, 3L), .Dimnames = list(
    NULL, c("Group", "EffectSize", "RequiredReplicates"))), structure(c("26000 Vs 26500", 
"26500 Vs 27000", "27000 Vs 27500", "0.0739021800199834", "0.14116830704947", 
"0.135704984161555", "2874", "788", "852"), .Dim = c(3L, 3L), .Dimnames = list(
    NULL, c("Group", "EffectSize", "RequiredReplicates"))))
names(my_list) <- paste0("tt", 1:3)

What I wanted is add a new column grp with the dataframe name and rbind them all to make one dataframe.

  lapply(
      my_list,
      function(x) {
      x$grp <- deparse(substitute(x))
      rbind(x)
    }
  )

Result I want:

Group            EffectSize           RequiredReplicates       grp
  "23000 Vs 23500" "1.03546847852537"   "15"                   tt1
  "23500 Vs 24000" "0.735744771309744"  "29"                   tt1
  "23500 Vs 24000" "0.735744771309744"  "29"                   tt2
  "24000 Vs 25000" "1.48620682621918"   "7"                    tt2
  "25000 Vs 25500" "0.418877850096638"  "89"                   tt2
  "26000 Vs 26500" "0.0739021800199834" "2874"                 tt3
  "26500 Vs 27000" "0.14116830704947"   "788"                  tt3
  "27000 Vs 27500" "0.135704984161555"  "852                   tt3

Thanks for your help!

Upvotes: 0

Views: 41

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 270448

1) data.table Convert each component to data.table and then use rbindlist with idcol argument.

library(data.table)

my_list_nms <- setNames(my_list, paste0("tt", seq_along(my_list)))
rbindlist(lapply(my_list_nms, as.data.table), idcol = "id")

giving this data.table:

    id          Group         EffectSize RequiredReplicates
1: tt1 23000 Vs 23500   1.03546847852537                 15
2: tt1 23500 Vs 24000  0.735744771309744                 29
3: tt2 23500 Vs 24000  0.735744771309744                 29
4: tt2 24000 Vs 25000   1.48620682621918                  7
5: tt2 25000 Vs 25500  0.418877850096638                 89
6: tt3 26000 Vs 26500 0.0739021800199834               2874
7: tt3 26500 Vs 27000   0.14116830704947                788
8: tt3 27000 Vs 27500  0.135704984161555                852

2) purrr Using purrr and tibble it can also be done. my_list_nms is from above.

library(purrr)
library(tibble)

map_dfr(my_list_nms, as_data_frame, .id = "id")

giving this tibble:

# A tibble: 8 x 4
  id    Group          EffectSize         RequiredReplicates
  <chr> <chr>          <chr>              <chr>             
1 tt1   23000 Vs 23500 1.03546847852537   15                
2 tt1   23500 Vs 24000 0.735744771309744  29                
3 tt2   23500 Vs 24000 0.735744771309744  29                
4 tt2   24000 Vs 25000 1.48620682621918   7                 
5 tt2   25000 Vs 25500 0.418877850096638  89                
6 tt3   26000 Vs 26500 0.0739021800199834 2874              
7 tt3   26500 Vs 27000 0.14116830704947   788               
8 tt3   27000 Vs 27500 0.135704984161555  852    

Upvotes: 1

Related Questions