Cactus
Cactus

Reputation: 924

Get indices of common rows from two different dataframes

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.

Upvotes: 6

Views: 3255

Answers (2)

Onyambu
Onyambu

Reputation: 79338

Just do

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

Upvotes: 1

RolandASc
RolandASc

Reputation: 3923

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

Upvotes: 5

Related Questions