8

I have 12 datasets that all resemble this (this is a sample, the real datasets all contain over 10,000 varying rows, with the same number/name of columns)

   df1

   Start                End                      Duration

   9/10/2019 1:00:00  PM  9/10/2019 1:00:10  PM   10
   10/10/2019 2:00:00 PM 10/10/2019 2:00:10  PM   10


   df2

   Start                End                        Duration

   11/10/2019 1:00:00 AM  11/10/2019 1:00:10  AM   10
   12/10/2019 2:00:00 AM  12/10/2019 2:00:10  AM   10


    df3

   Start                   End                   Duration

   01/10/2020 1:00:00 AM   01/10/2020 1:00:10 AM   10
   02/10/2020 2:00:00 AM   02/10/2020 2:00:10 AM   10

I would like this outcome:

   Start                     End                   Duration

   9/10/2019  1:00:00 PM    9/10/2019 1:00:10  PM   10
   10/10/2019 2:00:00 PM    10/10/2019 2:00:10 PM   10
   11/10/2019 1:00:00 AM    11/10/2019 1:00:10 AM   10
   12/10/2019 2:00:00 AM    12/10/2019 2:00:10 AM   10
   01/10/2020 1:00:00 AM    01/10/2020 1:00:10 AM   10
   02/10/2019 2:00:00 AM    02/10/2019 2:00:10 AM   10

Here is my dput:

 structure(list(Start = structure(1:2, .Label = c("11/10/2019 13:00", 
 "12/10/2019 14:00"), class = "factor"), End = structure(1:2, .Label = c("11/10/2019 13:00", 
 "12/10/2019 14:00"), class = "factor"), Duration = c(10L, 10L
 )), class = "data.frame", row.names = c(NA, -2L))



 structure(list(Start = structure(1:2, .Label = c("11/10/2019 1:00:00 AM", 
 "12/10/2019 2:00:00 AM"), class = "factor"), End = structure(1:2, .Label = c("11/10/2019 1:00:10 AM", 
"12/10/2019 2:00:10 AM"), class = "factor"), Duration = c(10L, 
 10L)), class = "data.frame", row.names = c(NA, -2L))


 structure(list(Start = structure(1:2, .Label = c("1/10/2020 1:00:00 AM", 
 "2/10/2020 2:00:00 AM"), class = "factor"), End = structure(1:2, .Label =     c("1/10/2020 1:00:10 AM", 
 "2/10/2020 2:00:10 AM"), class = "factor"), Duration = c(10L, 
 10L)), class = "data.frame", row.names = c(NA, -2L))

This is what I have tried:

  combined <- rbind(df1, df2)

However, it only works when joining 2 datasets and not 10

Lynn
  • 4,292
  • 5
  • 21
  • 44
  • 4
    We can use `do.call(rbind, lst1)` Place the datsets in a list. `do.call(rbind, mget(ls(pattern = "^df\\d+$")))` – akrun Feb 12 '20 at 23:21
  • your `dput` data doesn't include all the rows that you want in your output. It would be a lot easier to test solutions if the full output was there. – Dylan_Gomes Feb 12 '20 at 23:21
  • ok I will add the other 2 dput – Lynn Feb 12 '20 at 23:28
  • ok @akrun, I do not understand where I would put the dataset names in? So I would type out: do.call(rbind, lst1) and then do.call(rbind, mget(lst1(pattern = "df1,df2,df3,df4,df5,df6") ??? – Lynn Feb 12 '20 at 23:44
  • 1
    If you have created objects name as `df1`, `df2` etc. the above code would work i.e. pattern = "^df\\d+$"` – akrun Feb 13 '20 at 00:15
  • 1
    Please check the output of `mget(ls(pattern = "^df\\d+$"))` it will be all of the data.frames that created in a list – akrun Feb 13 '20 at 00:16

1 Answers1

6

You can use the bind_rows trom the tidyverse

library(tidyverse)
dim(mtcars) # [1] 32 11
BindMtcars <- bind_rows(mtcars, mtcars, mtcars, mtcars)
dim(BindMtcars) # [1] 128  11
Orlando Sabogal
  • 1,470
  • 7
  • 20