Let's say I have the following data.frame
test_df = read.table(text = "id, chat
11, hey how are you im doing well
12, hey how are you im
13, my name is bob
14, my name is bob. how are you?
15, test test
16, test testing
17, who is this
18, who is this. who are you?", header = T, sep = ",")
I'm trying to identify which pairs of rows, for the column chat have the most similar values via string distance. I then want to create a column partner_id which identifies the id of the person with the most similar value of chat.
Here's the desired output:
final_df = read.table(text = "id, chat, partner_id
11, hey how are you im doing well, 12
12, hey how are you im, 11
13, my name is bob, 14
14, my name is bob. how are you?, 13
15, test test, 16
16, test testing, 15
17, who is this, 18
18, who is this. who are you?, 17", header = T, sep = ",")
I know that the stringdist library has various fuzzy matching calculations, but I can't think of a better solution than just iterating over all the rows, computing every pairwise string distance, and then storing the best solution (which becomes computationally challenging with big data.frames). It's particularly challenging since every id can only have exactly one partner_id, so once a "best" match has been selected, that id should no longer be available for matching.