Assuming the following data frame:
df <- (data.frame(ID = c("1", "2", "3"),
Drink = c("Cola", "Pepsi", "Fanta")))
Output:
ID Drink
1 1 Cola
2 2 Pepsi
3 3 Fanta
I then have another data frame that looks something like this:
df2 <- (data.frame(Drink = c("Cola", "Fanta", "Cola", "Pepsi", "Pepsi")))
Drink
1 Cola
2 Fanta
3 Cola
4 Pepsi
5 Pepsi
I would like to create an extra column in this data frame that contains the IDs corresponding to whatever drink it specified in the row (based on the IDs in the first data frame (df)). So that the outcome looks like this:
ID Drink
1 1 Cola
2 3 Fanta
3 1 Cola
4 2 Pepsi
5 2 Pepsi