从两个不同的dataframes获取公共行的索引。

时间:2022-06-13 13:02:35

I have two dataframes:

我有两个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.

我需要找到dataframe df1中的行索引,其中的列cola和colb是相同的,这里是第1行和第2行。我从dplyr包中知道inner_join函数,但是这会产生一个新的dataframe。我只需要一个带指标的向量。对于需要的每一列,我都可以这样做,但是如果我需要基于大量的列找到公共行,这就太糟糕了。

Any help is much appreciated.

非常感谢您的帮助。

2 个解决方案

#1


3  

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]1 2 NA

#2


0  

Just do

只做

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

#1


3  

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]1 2 NA

#2


0  

Just do

只做

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