0

I have 5 tables containing the same columns A B C (same names of columns)

I want to create a data frame where i merge all of the five tables , I tried the command

My_Data <- data.frame(T1,T2,T3,T4,T5,T6) 

but i got duplicating columns like this

A  B  C  A1 B1 C1 A2 B2 C2 A3 B3 C3 A4 B4 C4
12 2  1  12 1  5  12 12 2  1   8  9  7 2   12
1  78 2  34 5  1  4  45 4  5   4  4  6 4   3
4  2  3  12 2  12 6  23 12 45 12  23 8 5   4

As you can see the number of columns duplicated 5 ( 35 = 15 ),but instead of this result, my desire output is like:

A  B  C
12 2  1  
1  78 2  
4  2  3
12 1  5
34 5  1
12 2  12 
...
..
Ravi Saroch
  • 934
  • 2
  • 13
  • 28
Reda
  • 449
  • 1
  • 4
  • 17
  • [This question](https://stackoverflow.com/questions/16138693/rbind-multiple-data-sets) may help. `do.call(rbind, list(T1, T2, T3,T4,T5,T6))` – maydin Dec 08 '20 at 08:42
  • Does this answer your question? [Combine (rbind) data frames and create column with name of original data frames](https://stackoverflow.com/questions/15162197/combine-rbind-data-frames-and-create-column-with-name-of-original-data-frames) – Ravi Saroch Dec 08 '20 at 08:49
  • Hello , thank you for your comment , yesl the command works well Thank you very much – Reda Dec 08 '20 at 09:12

1 Answers1

0

You can split based on the letters, stck them and column bind them, i.e.

l1 <- split.default(df, gsub('\\d+', '', names(df)))
setNames(do.call(cbind, lapply(l1, function(i)stack(i)[1])), names(l1))

#    A  B  C
#1  12  2  1
#2   1 78  2
#3   4  2  3
#4  12  1  5
#5  34  5  1
#6  12  2 12
#7  12 12  2
#8   4 45  4
#9   6 23 12
#10  1  8  9
#11  5  4  4
#12 45 12 23
#13  7  2 12
#14  6  4  3
#15  8  5  4
Sotos
  • 51,121
  • 6
  • 32
  • 66