Я анализирую доходность акций и имею 1 фрейм данных с тикерами и весами позиций и еще один фрейм данных с доходами. Я включил пример ниже. Мне нужно извлечь данные возврата из df 2 в пустой столбец в df 1 на основе кода тикера и даты. Это пример; Есть еще много тикеров и дат. Я пробовал методы из предыдущих постов безуспешно. Я новичок в Р. Может кто-нибудь помочь? Спасибо!
Df 1
+-----------+-----------+--------+
| Date code | Ticker | Return |
+-----------+-----------+--------+
| 1 | Ticker 3 | |
| 1 | Ticker 4 | |
| 1 | Ticker 5 | |
| 2 | Ticker 1 | |
| 2 | Ticker 10 | |
| 2 | Ticker 8 | |
| 3 | Ticker 9 | |
| 3 | Ticker 3 | |
| 3 | Ticker 7 | |
| 4 | Ticker 5 | |
| 4 | Ticker 5 | |
| 4 | Ticker 10 | |
| 5 | Ticker 8 | |
| 5 | Ticker 1 | |
| 5 | Ticker 7 | |
| 6 | Ticker 3 | |
| 6 | Ticker 9 | |
| 6 | Ticker 1 | |
| 7 | Ticker 6 | |
| 7 | Ticker 8 | |
| 7 | Ticker 3 | |
| 8 | Ticker 4 | |
| 8 | Ticker 5 | |
| 8 | Ticker 3 | |
| 9 | Ticker 5 | |
| 9 | Ticker 3 | |
| 9 | Ticker 9 | |
| 10 | Ticker 5 | |
| 10 | Ticker 5 | |
| 10 | Ticker 3 | |
+-----------+-----------+--------+
Df 2
+-----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+-----------+
| Date code | Ticker 1 | Ticker 2 | Ticker 3 | Ticker 4 | Ticker 5 | Ticker 6 | Ticker 7 | Ticker 8 | Ticker 9 | Ticker 10 |
+-----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+-----------+
| 1 | 0% | -3% | 3% | 1% | -3% | -1% | 0% | 0% | -3% | 0% |
| 2 | -2% | 1% | -2% | -3% | -1% | -2% | -1% | -2% | -3% | -1% |
| 3 | 2% | -2% | 2% | 1% | -1% | 2% | 0% | 3% | -3% | 1% |
| 4 | 1% | -2% | 2% | -1% | 0% | 0% | -2% | -3% | 3% | 3% |
| 5 | 3% | -2% | 1% | 0% | 0% | -1% | 0% | 3% | 3% | 0% |
| 6 | -3% | -3% | 0% | 2% | 0% | -3% | 0% | 0% | -3% | -2% |
| 7 | -1% | -2% | -2% | -1% | 3% | -3% | -3% | -2% | 2% | -3% |
| 8 | 0% | 1% | 2% | 2% | -2% | -3% | -3% | 3% | 3% | -3% |
| 9 | -2% | 2% | 3% | 2% | 1% | 3% | 0% | 2% | 1% | -3% |
| 10 | 2% | -2% | -2% | 0% | -2% | 1% | 1% | -3% | 3% | 1% |
+-----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+-----------+
Excepted result:
+-----------+-----------+--------+
| Date code | Ticker | Return |
+-----------+-----------+--------+
| 1 | Ticker 3 | 3% |
| 1 | Ticker 4 | 1% |
| 1 | Ticker 5 | -3% |
| 2 | Ticker 1 | -2% |
| 2 | Ticker 10 | -1% |
| 2 | Ticker 8 | -2% |
| 3 | Ticker 9 | -3% |
| 3 | Ticker 3 | 2% |
| 3 | Ticker 7 | 0% |
| 4 | Ticker 5 | 0% |
| 4 | Ticker 2 | -2% |
| 4 | Ticker 10 | 3% |
| 5 | Ticker 8 | 3% |
| 5 | Ticker 1 | 3% |
| 5 | Ticker 7 | 0% |
| 6 | Ticker 3 | 0% |
| 6 | Ticker 9 | -3% |
| 6 | Ticker 1 | -3% |
| 7 | Ticker 6 | -3% |
| 7 | Ticker 8 | -2% |
| 7 | Ticker 3 | -2% |
| 8 | Ticker 4 | 1% |
| 8 | Ticker 5 | -2% |
| 8 | Ticker 3 | 2% |
| 9 | Ticker 5 | 1% |
| 9 | Ticker 3 | 3% |
| 9 | Ticker 9 | 1% |
| 10 | Ticker 5 | -2% |
| 10 | Ticker 9 | 3% |
| 10 | Ticker 3 | -2% |
+-----------+-----------+--------+