I would like to find matching observations in two different datasets based on two variables.
The first dataset "df1" exists of the following two variables:
SessionID   MarkerID
14  5
14  5
14  5
14  8
17  9
17  9
17  8
17  2
17  9
The othere dataset "df2" exists of the same two variables
SessionID   MarkerID
14  5
17  8
17  2
Now, I would like to add another variable "Match" to df1 that shows if a match was found between the two datasets (Match = 1) or not (Match = 0) for an observation. The observation should have the same value for both the SessionID AND MarkerID.
The desired output looks as follows:
SessionID   MarkerID    Match
14  5    1
14  5    1
14  5    1
14  8    0
17  9    0
17  9    0
17  8    1
17  2    1
17  9    0
Reproducable example:
SessionID <- c(14,14,14,14,17,17,17,17,17)
MarkerID <- c(5,5,5,8,9,9,8,2,9)
df1 <- as.data.frame(cbind(SessionID, MarkerID))
SessionID <- c(14,17,17)
MarkerID <- c(5,8,2)
df2 <- as.data.frame(cbind(SessionID,MarkerID))
I have tried the following code but it did not produce the desired output:
df1$Match <- 0 
df1$Match[which(df1$MarkerID == df2$MarkerID & df1$SessionID == df2$SessionID )] <- 1