I have two data frames: A
with 189 rows and 79 columns, and B
with 354 rows and 2 columns. Some of their row names match approximately like below:
A:
Sample value1 value2 value 3
10003 a b d
10003_Qi1 a a c
10003_Qi2 b a c
10017 b g c
10018 b f s
10025_Qi o w c
10040_Qi1 x y o
10040_ArT1 e g g
10125 p g m
10140_Ar1 w n c
10225 z c p
B:
Sample first
10003 4
10004 8
10018 45
10025 85
10032 7
10040 54
10140 2
10132 8
10200 65
10324 9
10400 32
I want to merge the two data frames based on an inexact match of their row names, which would result in this:
Sample value1 value2 value 3 first
10003 a b d 4
10018 b f s 45
10025_Qi o w c 85
10040_Qi1 x y o 54
10140_Ar1 w n c 2
The rule is:
If they match exactly, otherwise
If they match on the first five numbers, with
a.
_Qi1
and_Qi2
inA
, the sample inB
gets the sample value inA
with_Qi1
b.
_Qi1
and_ArT1
inA
, the sample inB
gets the sample value inA
with_Qi1
and merge