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!!