I have two datasets with the same columns, but different values in the columns. I want to be able to perform an operation similar to "Index-match" in Excel such that a row from one dataset matches up to it's equivalent row in the other so that I can perform operations on the lined up data.
For example,
#create example dataframes
d1 <- data.frame(country = c('US','US','US','DE','DE'),
type = c('A','B','C','A','B'),
val = c(1,6,3,3,7))
d2 <- data.frame(country = c('US','DE','US','BR','DE'),
type = c('B','C','A','B','C'),
val = c(2,2,33,0,9))
I would like to match the rows in d1
that correspond to d2
and do d1$val - d2$val
and save a new dataframe, d3
that will be only the rows who's key columns matched.
In this example, d1[1,1:2]
matches d2[3,1:2]
so they should line up,etc.