Skip to content
Prev 299863 / 398503 Next

help with merging 2 data frames

This should do the trick :

colnames(y)[1:2]=c("a","a")
y2=rbind(y[,-1], y[,-2]) #duplicating the "y" matrix so the identifiers 
are only in 1 column
merged = merge(x,y2)

merged
    a b  d  e1  e2
1 aa 1 10 100 101
2 aa 2 20 200 201
3 ab 1 30 100 101
4 ab 2 40 200 201
5 ba 1 50 300 301
6 ba 2 60 400 401
7 bb 1 70 300 301
8 bb 2 80 400 401

desired
    a b  d  e1  e2
1 aa 1 10 100 101
2 aa 2 20 200 201
3 ab 1 30 100 101
4 ab 2 40 200 201
5 ba 1 50 300 301
6 ba 2 60 400 401
7 bb 1 70 300 301
8 bb 2 80 400 401

  all(merged==desired)
[1] TRUE

Cheers,

Eloi
On 12-07-11 03:50 PM, Dimitri Liakhovitski wrote: