Compare two frames in parts of R

I want to compare two frames of data in parts. Here is an example of my data frames:

a1 <- data.frame(a = 1:5, b=letters[1:5])
a2 <- data.frame(a = c(1,6,3,4), b=letters[1:4])

I would like to write a function that finds two consecutive rows in a1, which also exists in a2 data frame (both columns must match) and save it in a new frame.

Any help would be appreciated.

+4
source share
1 answer
dual.matches <- match(a1$a, a2$a) == match(a1$b, a2$b) 
sequential.dual.matches <- with(rle(dual.matches), rep(replace(values, lengths==1, FALSE), lengths))

a1[sequential.dual.matches, ]
#   a b
# 3 3 c
# 4 4 d
+3
source

All Articles