[R] Merge problem

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Sep 22 12:02:48 CEST 2006


On Fri, 22 Sep 2006, Tova Fuller wrote:

> Hello all,
>
> I have read as many merge issues as I possibly could tonight and
> although I presume this is a small error, I have not found the
> solution to my problem.
>
> I'm trying to merge two data sets: dat0 and TransTable.  As you can
> see below, dat0 has 8000 rows, whereas TransTable has 47296 rows.  I
> would expect when I merge the two data sets, with all.x=F, and
> all.y=F, that the intersection would yield 8000 rows, considering
> dat0 is a subset of TransTable.
>
> However, I get a neat little surprise when I check the dimensions of
> the resultant data frame - dat0merge, the merged data frame has 8007
> rows!  How can this be?  Where did these extra 7 rows come from?
> This appears to defy logic!

Not the help page, though.

      joined together.  If there is more than one match, all possible
      matches contribute one row each.

I presume you think you are doing a 1:1 match, but probably you have 
multiple matches for up to 7 ids.

This is one of the commonest misconceptions about merge that you will find 
frequently in the list archives.

>
> Thank you in advance for your help.  I've put my code below for
> reference.
>
> Tova Fuller
>
> > dim(dat0)
> [1] 8000   60
> > dim(TransTable)
> [1] 47296     9
> > dat0merge=merge(TransTable,dat0,
> by.x="Target",by.y="TargetID",all.x=F,all.y=F)
> > dim(dat0merge)
> [1] 8007   68
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list