[R] data.frame merge matching several columns

Thomas Lumley tlumley at u.washington.edu
Tue Oct 8 23:37:40 CEST 2002


On Tue, 8 Oct 2002, Patrick E. McKnight wrote:

> Greetings,
>
> Is it possible to match several columns in a merge statement?  Here is my
> problem:
>
> data1 looks like this...
>
> SUBID  TARGID  ITEM  RATING
> 1         1      1      4
> 1         1      2      5
> 1         1      3      3
> 1         1      4      2
> 1         1      5      5
> ......
>
> SUBID is the ID for the raters, TARGID is the ID for the targets being
> rated, ITEM ranges from 1 to 64 crossed by TARGID (i.e., all targets were
> rated on 64 items), and RATING is the rating given by SUBID for each
> TARGID and ITEM.
>
> My second dataset looks like this data2:
>
> TARGID  ITEM  TARGET
> 1        1      5
> 1        2      4
> 1        3      6
> 1        4      2
> 1        5      3
> .......
>
> TARGID again is the target's ID, ITEM ranges from 1 to 64, and TARGET is
> the rating provided by the target.
>
> I would like to merge these two data.frames by TARGID and ITEM to yield
> the following structure:
>
> SUBID  TARGID  ITEM  RATING  TARGET
> 1         1      1      4      5
> 1         1      2      5      4
> 1         1      3      3      6
> 1         1      4      2      2
> 1         1      5      5      3
> ........
>
> I realize that merge appears to be the proper tool but there is no
> documentation for how to merge by multiple columns.  Is this possible?
>

Yes. help(merge) says "By default the data frames are merged on the
columns with names they both have," which handles your example.

You could also do
  merge(x,y,by=c("TARGID","ITEM")
to specify the columns explicitly.

	-thomas







-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list