1

I have two tables and I need to union 2 columns in 1. Each column from a different table. I'd like to create a new table with column z where I will have every value from x & y with dublicates. Can you guys help me with that?

id x 
1  p
2  e
3  p 


id y
1  e
2 
3  e 
user438383
  • 5,716
  • 8
  • 28
  • 43
Angelo
  • 21
  • 3

3 Answers3

2

Are you looking for such a solution: here is an example step by step:

table1 <- tibble(id = c(1,2,3),
                 x= c("p", "e", "p"))
table1

table2 <- tibble(id = c(1,2,3),
                 x= c("e", NA, "e"))
table2

table3 <- bind_rows(table1, table2)
table3$z <- table3$x
table3 <- table3[,3]
table3


Output:

  z    
  <chr>
1 p    
2 e    
3 p    
4 e    
5 NA   
6 e    
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Doesn't really work ;/ I have already some data in those 2 column are the same but columns' names are different – Angelo May 09 '21 at 10:36
2

We can also use rbindlist

library(data.table)
rbindlist(list(table1, table2))
akrun
  • 874,273
  • 37
  • 540
  • 662
1

A base R option

> list2DF(Map(c, df1, df2))
  id    x
1  1    p
2  2    e
3  3    p
4  1    e
5  2 <NA>
6  3    e
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81