You want to join these two datasets together. data.table
is really good at this.
library(data.table)
setDT(dataset1)
setDT(dataset2)
setkey(dataset1, company)
setkey(dataset2, company)
dataset2[dataset1[, .(fname, lname, email, company)]]
Left joins
Left outer join
The result of a left outer join (or simply left
join) for tables A and B always contains all rows of the "left" table
(A), even if the join-condition does not find any matching row in the
"right" table (B). This means that if the ON clause matches 0 (zero)
rows in B (for a given row in A), the join will still return a row in
the result (for that row)—but with NULL in each column from B. A left
outer join returns all the values from an inner join plus all values
in the left table that do not match to the right table, including rows
with NULL (empty) values in the link column.
Wikipedia.
Here we are left joining dataset2 on dataset1. We keep all rows from dataset1, and join dataset2 if company from dataset1 matches company from dataset2. If it matches, we keep dataset2's row and add it, including the format field. If it doesn't match, we get NA
.