Home > Blockchain >  How to find common rows between two data frames?
How to find common rows between two data frames?

Time:04-05

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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
  • Related