0

I have 2 data frames. The first data frame has 2 columns (Ticker, Date), the second data frame has 3 columns (Ticker, Date, Price). The first data frame only has 1 row per Ticker while the 2nd data frame has many rows per Ticker. For example.

df1

Ticker   Date
GS       2019-01-01
AAPL     2019-02-19
GE       2019-02-14

df2

Ticker  Date        Price
GS      2019-01-01  100
GS      2019-10-10  105
AAPL    2019-02-19  210
AAPL    2019-05-05  225
GE      2019-02-14  28
GE      2019-02-21  27

I would like to group by ticker and extract only the row that has the same Date and ticker.

For example something like this.

df2 %>% group_by(Ticker, Date) %>% slice(#match df1 Ticker and Date)%>% un_group()

Ticker   Date        Price
GS       2019-01-01   100
AAPL     2019-02-19   210
GE       2019-02-14    28

Lastly, if you could show a way to do this in purrr that would be awesome. Thank you very much.

Jordan Wrong
  • 1,205
  • 1
  • 12
  • 32
  • Looks like you need `inner_join` `inner_join(df1, df2)` – akrun Jun 24 '19 at 22:11
  • OH! I see what your saying. Inner join by 2 columns! Thank you. I did not know I could do that! – Jordan Wrong Jun 24 '19 at 22:15
  • Is there a specific reason you don't want to use a JOIN? That might help us figure out the specific constraints you're operating under and help us make a better answer – divibisan Jun 24 '19 at 22:17
  • I did not know I was able to join by 2 columns. The link akrun originally posted, helped solve my issue – Jordan Wrong Jun 24 '19 at 22:19
  • @divibisan Can you close that link. I reopened after OP's comment and is not able to close it again – akrun Jun 24 '19 at 22:20
  • 1
    Great! For people coming by later, you can join on multiple columns by passing a vector to the `by=` argument – divibisan Jun 24 '19 at 22:21

1 Answers1

0

The solution for this problem was to use dplyr inner_join. I was able two specify to columns to join by.

#solution
inner_join(df1, df2, by = c("Ticker", "Date"))

You can actually pass a vector of columns into the by function. Thanks divibisan

Jordan Wrong
  • 1,205
  • 1
  • 12
  • 32