0

I've got two data frames, one that's quite long with historical data like below. It's got a Date, a label (the state) and a value. There are multiple entries for each day, but on each day, each state appears only once (e. g. Indiana will appear only once on 2020-01-20). We'll call this dataframe A:

Date            State      Score
2020-01-20      IN         10
2020-01-20      TX         5
2020-01-21      MI         8
2020-01-21      CA         7

I have a second date frame, much shorter in length, that has a select number of dates and states, along with some other random data. We'll call this dataframe B:

Date            State      Coach      Team
2020-01-20      IN         Jim        Tigers
2020-01-21      CA         Greg       Lions

I want to be able to add a new column to the second dataframe with the "Score" from the first dataframe, where the Date and the State match. So, my end goal is a dataframe that looks like this:

Date            State      Coach      Team       Score
2020-01-20      IN         Jim        Tigers     10
2020-01-21      CA         Greg       Lions      7

My first idea was using which:

A$Score[which(A$Date == "2020-01-20" & A$State == "IN")]

and that returns the correct "10"

However, something doesn't work correctly when I try to use references to dataframe B and add the new column:

B$Score <- A$Score[which(A$Date == B$Date & A$State == B$State)]

All sorts of errors pop up with this formula.

If I need to make this a loop function, I need some help--don't know loops very well at all. Thank you all!!

ragsy12
  • 5
  • 1
  • Use `merge(A, B)` Or `merge(A, B, by = c('Date', 'State'))` to be specific. – Ronak Shah Jan 29 '20 at 03:22
  • Ronak, thank you so much for the quick reply--that's so much easier than I thought, I had no idea that was a function. Makes my life very easy, now! – ragsy12 Jan 29 '20 at 03:27

0 Answers0