-1

I have several dataframes:

toto1_1 <- data.frame(x=1:3)
toto1_2 <- data.frame(x=1:3)
titi1_1 <- data.frame(x=1:3)
titi1_2 <- data.frame(x=1:3)

What is the best way to concatenate these tables using 2 different patterns?

Thank you.

cmaher
  • 5,100
  • 1
  • 22
  • 34
pupilR
  • 1
  • 1
  • 3
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. How were these data.frames created in the first place? Having variabls with numeric indexes in the name isn't a great idea. Most likely a better solution would involved a [list of data.frames](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames). – MrFlick Apr 09 '18 at 15:25
  • I would add the an expected output would increase the chance of receiving answers to the question in your mind – Seymour Apr 09 '18 at 15:33
  • not clear what you want – Andre Elrico Apr 09 '18 at 15:38
  • Ok. I will give more détails in the future. Thank you – pupilR Apr 10 '18 at 15:42

2 Answers2

0

The mget function will return a list of data-objects when given a character vector:

totoList <- mget( paste0( rep(c("toto","titi"),each=2), rep(c("1_1","1_2") ) ) 

str(totoList)
List of 4
 $ toto1_1:'data.frame':    3 obs. of  1 variable:
  ..$ x: int [1:3] 1 2 3
 $ toto1_2:'data.frame':    3 obs. of  1 variable:
  ..$ x: int [1:3] 1 2 3
 $ titi1_1:'data.frame':    3 obs. of  1 variable:
  ..$ x: int [1:3] 1 2 3
 $ titi1_2:'data.frame':    3 obs. of  1 variable:
  ..$ x: int [1:3] 1 2 3

If the goal were as single lust, then that could be an intermediate result on the way to:

do.call( "rbind", totoList)  # rbind transforms character value to an R function 
          x
toto1_1.1 1
toto1_1.2 2
toto1_1.3 3
toto1_2.1 1
toto1_2.2 2
toto1_2.3 3
titi1_1.1 1
titi1_1.2 2
titi1_1.3 3
titi1_2.1 1
titi1_2.2 2
titi1_2.3 3
IRTFM
  • 258,963
  • 21
  • 364
  • 487
0

please see some suggestions below. 1. They're concatenated using the rbind() function 2. They're concatenated using the c() function -- retains as separate vector elements but concatenated into a single element 3. Perhaps more useful because it preserves the table names of the original tables for future analysis and sorting: to add each into a list, then to create a data.frame for each table name and it's data; and then concatenate into a single object using rbind()

toto1_1 <- data.frame(x=1:3)
toto1_2 <- data.frame(x=1:3)
titi1_1 <- data.frame(x=1:3)
titi1_2 <- data.frame(x=1:3)

#1
rbind(toto1_1,toto1_2,titi1_1,titi1_2)

#2
c(toto1_1,toto1_2,titi1_1,titi1_2)

#3
l <- list(toto1_1=toto1_1,toto1_2=toto1_2,titi1_1=titi1_1,titi1_2=titi1_2)
do.call(rbind,lapply(names(l),FUN=function(x) { data.frame(table_name=x,table_data=l[[x]]) }))
Soren
  • 1,792
  • 1
  • 13
  • 16