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

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Aug 7 22:14:05 CEST 2006


"Mark Na" <mtb954 at gmail.com> writes:

> 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?

Doesn't sound like a merge problem. Will this do it?:

first[first$ID %in% second$ID,]

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list