The answers are still good. However, the question was asked: "What if there were no" UniqID "column?
At this point, merge can help:
Here is an example of using merge and %in% where the identifier is available:
set.seed(1) df1 <- data.frame(ID = 1:10, A = rnorm(10), B = rnorm(10)) df2 <- df1[sample(10, 4), ] temp <- merge(df1, df2, by = "ID")$ID df1$matches <- as.integer(df1$ID %in% temp)
And a similar example when the identifier is not available.
set.seed(1) df1_NoID <- data.frame(A = rnorm(10), B = rnorm(10)) df2_NoID <- df1_NoID[sample(10, 4), ] temp <- merge(df1_NoID, df2_NoID, by = "row.names")$Row.names df1_NoID$matches <- as.integer(rownames(df1_NoID) %in% temp)
source share