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

Gabor Grothendieck ggrothendieck at gmail.com
Tue Aug 8 02:37:14 CEST 2006


Although this is probably not directly applicable to this problem
I might mention here that merge.zoo does support left and right
joins and that handles problems similar to this.  z3t, z3ft,
z3tf and z3f below have times of both unioned, the times of
z2, the times of z1 and the times of both z1 and z2 intersected
respectively:

library(zoo)
z1 <- zoo(1:5, 1:5)
z2 <- zoo(2:6, 2:6)
z3t <- merge(z1, z2, all = TRUE)
z3ft <- merge(z1, z2, all = c(FALSE, TRUE))
z3tf <- merge(z1, z2, all = c(TRUE, FALSE))
z3f - merge(z1, z2, all = FALSE)


On 07 Aug 2006 22:14:05 +0200, Peter Dalgaard <p.dalgaard at biostat.ku.dk> wrote:
> "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
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list