[R] data.frame merge matching several columns

Patrick E. McKnight pem at theriver.com
Tue Oct 8 21:57:49 CEST 2002


Tony,

Right you are!  Apologies for overlooking such an obvious feature.  Thanks
for your help.

Cheers,

Patrick


> It looks like it works right out of the box.  (The documentation for
> merge() in R1.5.1 pretty clearly mentions the possibility of merging on
> multiple columns .)
>
>  > m1 <- read.table("tmp1.txt", header=T, row.names=NULL)
>  > m2 <- read.table("tmp2.txt", header=T, row.names=NULL)
>  > m1
>    SUBID TARGID ITEM RATING
> 1     1      1    1      4
> 2     1      1    2      5
> 3     1      1    3      3
> 4     1      1    4      2
> 5     1      1    5      5
>  > m2
>    TARGID ITEM TARGET
> 1      1    1      5
> 2      1    2      4
> 3      1    3      6
> 4      1    4      2
> 5      1    5      3
>  > merge(m1, m2)
>    TARGID ITEM SUBID RATING TARGET
> 1      1    1     1      4      5
> 2      1    2     1      5      4
> 3      1    3     1      3      6
> 4      1    4     1      2      2
> 5      1    5     1      5      3
>  >
>
> At 11:17 AM 10/8/2002 -0700, you 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?
>>
>>Thanks in advance for any assistance with the problem.
>>
>>
>>--
>>Cheers,
>>
>>Patrick
>>
>>
>>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
>> 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
>> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>>


--
Cheers,

Patrick


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