简体   繁体   中英

How do you merge tables by ID, then combine cells with the same ID

Table1:

      ID Sex
1: 12345   M
2: 23456   M
3: 34567   F
4: 45678   F
5: 56789   F

Table2:

      ID  name
1: 12345   sam
2: 23456  jack
3: 23456   tom
4: 56789 steve
5: 56789   rob

I need to merge these two tables to get:

   ID Sex      name
1: 12345   M       sam
2: 23456   M  jack,tom
3: 34567   F          
4: 45678   F          
5: 56789   F steve,rob

I tried to use the Join function but it doesn't allow me to combine those names, is there a good way to solve this?

If these are data.tables, one option would be to paste the 'name' by 'ID' in the second dataset 'dt2' and join with the first ('dt1') on 'ID' and create the column 'name' in the first dataset

library(data.table)
dt1[dt2[, .(name = toString(name)), ID], name := name, on = .(ID)]
dt1
#      ID Sex       name
#1: 12345   M        sam
#2: 23456   M  jack, tom
#3: 34567   F       <NA>
#4: 45678   F       <NA>
#5: 56789   F steve, rob

This is a combination of three separate operations that have been covered in detail in other answers on this site:

  1. You want to join the tables together :

  2. then spread from long to wide format

  3. and finally paste those multiple 'name' columns together into a single name column

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM