[R] Retain only those records from a dataframe that exist in another dataframe

Marc Schwartz (via MN) mschwartz at mn.rr.com
Mon Aug 7 22:17:27 CEST 2006

On Mon, 2006-08-07 at 14:05 -0600, Mark Na wrote:
> Dear R community,
> I have two dataframes "first" and "second" which share a unique identifier.
> I wish to make a new dataframe "third" retaining only the rows in
> "first" which also occur in "second".
> I have tried using merge but can't seem to figure it out. Any ideas?
> Thanks!
> Mark

Do you want to actually join (merge) matching rows from 'first' and
'second' into 'third', or just get a subset of the rows from 'first'
where there is a matching UniqueID in 'second'?

In the first case:

  third <- merge(first, second, by = "UniqueID")

Note that the UniqueID column is quoted.

In the second case:

  third <- subset(first, UniqueID %in% second$UniqueID)

See ?merge, ?"%in%" and ?subset


Marc Schwartz

More information about the R-help mailing list