Question

I have two data as follows. I want to transfer the value of change in the first dataframe to the second dataframe by date and code.

> a
    date       code            change
 1 2013-12-27 000586 0.990099012851715
 2 2013-12-26 000616  9.97067451477051
 3 2013-12-25 300295  10.0013799667358
 4 2013-12-23 000968  -8.1564245223999
 5 2013-12-19 600023  60.5786628723145
 6 2013-12-18 600855 -7.69696950912476

> b

     code    date       company
 1 000586 2013-12-27       a
 2 000616 2013-12-26       b
 3 300295 2013-12-25       c
 4 000968 2013-12-23       d
 5 600023 2013-12-19       a
 6 600023 2013-12-19       b
 7 600855 2013-12-18       c

The final result should look like:

> c
      code       date company change
1 000586 2013-12-27       a    0.990099012851715
2 000616 2013-12-26       b      9.97067451477051
3 300295 2013-12-25       c     10.0013799667358
4 000968 2013-12-23       d      -8.1564245223999
5 600023 2013-12-19       a     60.5786628723145
6 600023 2013-12-19       b     60.5786628723145
7 000586 2013-12-27       b      0.990099012851715

How can get it this?

Was it helpful?

Solution

Instead of creating a new dataframe, you can also add a new column to your b dataframe with match:

b$change <- a$change[match(b$code,a$code)]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top