F_Keikha
F_Keikha

Reputation: 29

How to find common rows between two data frames?

I have two data frames in R:

My goal is two find the common rows between two data frames according to two columns and select them in the first one. For example, I have the below data frames:

c<- data.frame(A = c(4,6,7), B = c(5,9,8),C = c("T","T","F"))
d<- data.frame(A = c(6,7,3),B = c(9,8,3))

And I want the result to be:

# A B C
# 6 9 T
# 7 8 F

Upvotes: 0

Views: 100

Answers (3)

Allan Cameron
Allan Cameron

Reputation: 174641

You can do an inner join on all columns:

dplyr::inner_join(c, d, by = c('A', 'B', 'C'))
#>   A B C
#> 1 6 9 T
#> 2 7 8 F

Upvotes: 0

Sweepy Dodo
Sweepy Dodo

Reputation: 1883

library(data.table)

# set as data table
lapply(list(c,d), \(i)setDT(i))

# join
c[d, on=names(c), nomatch=0][1]

I have a feeling I may not have understood fully when you said "select them in the first one". If so, please explain

Upvotes: 0

Quinten
Quinten

Reputation: 41603

You can use the following code:

c<- data.frame(A = c(4,6,7), B = c(5,9,8),C = c("T","T","F"))
d<- data.frame(A = c(6,7,3),B = c(9,8,3),C = c("T","F","F"))

merge(c, d, by= c("A", "B", "C"))

Output:

  A B C
1 6 9 T
2 7 8 F

Upvotes: 1

Related Questions