I may be misunderstanding, but doesn't this do what you want (i.e. each data.frame with only the values of the first column present in the first column of the other)?
A[A$x_1 %in% B$x_2,]# x_1 y_1# 1 A1 1.00# 2 A1 3.56# 3 B10 6.11# 4 B10 8.67# 5 B10 11.22# 6 B10 13.78# 7 C100 16.33# 8 C100 18.89# 9 C100 21.44# 10 C100 24.00B[B$x_2 %in% A$x_1,]# x_2 y_2# 1 A1 1.0# 2 B10 5.6# 3 C100 10.2
Also, if you want to connect them, you can use either of these two approaches:
cbind(A, y_2=B[match(A$x_1, B$x_2), "y_2"])merge(A, B, by=1) #
With the first one faster than the second, though both producing the same output. The match
method is much faster, but has the limitation that the B
table has to be unique on what your joining on (which is the case here).
# x_1 y_1 y_2# 1 A1 1.00 1.0# 2 A1 3.56 1.0# 3 B10 6.11 5.6# 4 B10 8.67 5.6# 5 B10 11.22 5.6# 6 B10 13.78 5.6# 7 C100 16.33 10.2# 8 C100 18.89 10.2# 9 C100 21.44 10.2# 10 C100 24.00 10.2
Finally, this replicates newB_1
:
B[match(A$x_1, B$x_2), ]# x_2 y_2# 1 A1 1.0# 1.1 A1 1.0# 2 B10 5.6# 2.1 B10 5.6# 2.2 B10 5.6# 2.3 B10 5.6# 3 C100 10.2# 3.1 C100 10.2# 3.2 C100 10.2# 3.3 C100 10.2