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

From: Mark Na <mtb954_at_gmail.com>
Date: Tue 08 Aug 2006 - 06:49:23 EST

Thanks Peter and Mark, the subset and %in% commands did the job.

For future reference, or for others reading this message, the code I ended up using was:

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

Mark

On 8/7/06, Marc Schwartz (via MN) <mschwartz@mn.rr.com> wrote:
> 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
>
> HTH,
>
> Marc Schwartz
>
>
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue Aug 08 07:58:23 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 08 Aug 2006 - 08:21:14 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.