R - Add a new column to a dataframe using matching values of another dataframe

merge(table1, table2[, c("pid", "val2")], by="pid")

Add in the all.x=TRUE argument in order to keep all of the pids in table1 that don't have matches in table2...

You were on the right track. Here's a way using match...

table1$val2 <- table2$val2[match(table1$pid, table2$pid)]


I am not sure if you mean this but you might use:

newtable <- merge(table1,table2, by  = "pid") 

This will create a new table called newtable, with 3 columns and those values matched by the id, in this case "pid".


I'm way late here, but in case anybody else asks the same question:
This is exactly what dplyr's inner_merge does.

table1.df <- dplyr::inner_join(table1, table2, by=pid)

The by-command specifies which column should be used to match the rows.

EDIT: I used to have so much difficulty remembering it's a [join], and not a [merge].

Tags:

R

Match

Dataframe