[R] Problems with merge

Thomas Lumley tlumley at u.washington.edu
Wed Oct 6 17:13:10 CEST 2004


On Wed, 6 Oct 2004, Vikas Rawal wrote:
>
> The problem is that R allows you to use by.x and by.y variables to specify 
> only one variable in x dataset and one variable in y dataset to merge.

This turns out not to be the case.

> names(df)
[1] "x" "y" "z"
> names(df2)
[1] "a" "b" "c"
> merge(df,df2,by.x=c("x","y"),by.y=c("a","b"))
      x   y   z   c
1  101 111 121 221
2  102 112 120 220
3  103 113 121 221
4  104 114 120 220
5  105 115 121 221
6  106 116 120 220
7  107 117 121 221
8  108 118 120 220
9  109 119 121 221
10 110 120 120 220


> If nothing else works, that is what I shall have to do. There again we have 
> some problem. How do I change the name of a particular column. One solution 
> suggested somewhere in the archives of the list is to use
>

names(df)[names(df) == "oldname"] <- "newname"

is one possibility that doesn't even require working out which variable 
number it is.

 	-thomas




More information about the R-help mailing list