-2

I want to compare rownames in one matrix (df2) with column values in another dataframe (df1). after comparing i want to add the values of df2 to df1 in new column.

df1
ID  value  
A    12
B    13
C    14
A    15


df2
    LocationID
A    2
B    3
C    4

Finalresult
ID   Value   LocationID
A     12        2
B     13        3
C     14        4
A     15        2
mnel
  • 113,303
  • 27
  • 265
  • 254
  • 4
    Welcome to SO. You'll find you get much better responses if you provide some sample data and a clear and specific description of your problem. [See here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Questions like this one are too vague to answer. – Justin Dec 20 '12 at 21:26
  • 2
    Dont be discouraged though. Please feel free to re-work your question as per @Justin's suggestion – Ricardo Saporta Dec 20 '12 at 22:20

1 Answers1

2

You are looking for merge. You can set by.y to inspect the rownames, by.x to ID

merge(df1, df2, by.x = 'ID', by.y = 'row.names')
  ID value LocationID
1  A    12          2
2  A    15          2
3  B    13          3
4  C    14          4
mnel
  • 113,303
  • 27
  • 265
  • 254