4
votes

I have two dataframes:

df1 <- data.frame(cola = c("dum1", "dum2", "dum3"), colb = c("bum1", "bum2", "bum3"), colc = c("cum1", "cum2", "cum3"))

and:

df2 <- data.frame(cola = c("dum1", "dum2", "dum4"), colb = c("bum1", "bum2", "bum3"))

I need to find the indices of the rows in dataframe df1 in which the columns cola and colb are the same, here it would be row 1 and row 2. I know the inner_join function from the dplyr package but this results in a new dataframe. I just need a vector with the indices. I could do this with which for each column needed but this would be ugly if I need to find common rows based on a large number of columns.

Any help is much appreciated.

2
Related: How do I tag rows with two variables that match rows in a second data frame? R, where output is logical instead of index. - Henrik

2 Answers

4
votes

The more general typical way of solving this would look like:

colsToUse <- intersect(colnames(df1), colnames(df2))
match(do.call("paste", df1[, colsToUse]), do.call("paste", df2[, colsToUse]))

[1] 1 2 NA

1
votes

Just do

 which(apply(df1[1:2]==df2,1,prod)==1)