The Smart Chef

data.table Join

Here's an example of joining two data tables in R. This is incredibly useful and similar to a "left join" in sql.

data.table join

  

# Load the data.table library
library(data.table)

# Create a People data.table
people <- data.table(id = c(1, 2, 3, 4, 5), name = c("Randy", "Will", "Ben", "Katie", "Amy"))

# Create a Visits data.table
visits <- data.table(id = c(1, 2, 2, 4, 4, 5, 5, 5), visit_date = c("2023-01-01", "2023-01-01", "2023-02-01",
                                                                    "2023-02-03", "2023-02-06", "2023-02_10",
                                                                    "2023_02-11", "2023-02-12"))

print(people)
   id  name
1:  1 Randy
2:  2  Will
3:  3   Ben
4:  4 Katie
5:  5   Amy

print(visits)
   id   visit_date
1:  1   2023-01-01
2:  2   2023-01-01
3:  2   2023-02-01
4:  4   2023-02-03
5:  4   2023-02-06
6:  5   2023-02_10
7:  5   2023_02-11
8:  5   2023-02-12

## Show the visits, with person name from People table
data <- visits[people, on = "id", nomatch = 0L]

> print(data)
   id visit_date  name
1:  1 2023-01-01 Randy
2:  2 2023-01-01  Will
3:  2 2023-02-01  Will
4:  4 2023-02-03 Katie
5:  4 2023-02-06 Katie
6:  5 2023-02_10   Amy
7:  5 2023_02-11   Amy
8:  5 2023-02-12   Amy


  

If you want to keep all of the records in your main table, even if they don't exist in your other table, you can omit the nomatch = 0L

If you have a SQL background, this would be similar to a left outer join. Keep in mind, however, that the "Left" table is actually on the right in this statement