【发布时间】:2021-09-29 07:44:11
【问题描述】:
我正在尝试将 df_1 数据帧中的 col_1 与 df_2 数据帧中的 col_2 进行比较,以获得最接近前 3 名的最低分数匹配(最低分数代表最接近的匹配)及其各自的 rowid。 还有任何灵活性可以更改前 N 个最接近的匹配项。即在我的情况下,我考虑了前 3 名,并像前 5 名、前 10 名等一样进行更改
col_1 = c("My name is john","The best ever Puma wishlist", "i have been mailing my issue daily",
"Its perfect for a day at gym")
col_2 = c("My name is jon","My Name is jhn", "My Nam is mark", "Mu Name is John",
"John is my name", "Its perfect for a day at gym&outside", "Its perfect for a outside",
"Its perfect day at gym", "Its perfect for a day at gm", "My name is john" )
row_id = c(1,2,3,4,5,6,7,8,9,10)
df_1 = data.frame(col_1)
df_2 = data.frame(col_2,row_id)
最终的df应该是
col_1 = c("My name is john","The best ever Puma wishlist","i have been mailing my issue daily","Its perfect for a day at gym")
nearest_1 = c("My name is John","Its perfect for a outside","Its perfect for a day at gym&outside","Its perfect for a day at gm")
nearest_1_row_id = c(10,7,6,9)
nearest_2 = c("My name is jon","Its perfect for a day at gym&outside","John is my name","Its perfect for a day at gym&outside")
nearest_2_row_id = c(1,6,5,6)
nearest_3 = c("My Name is jhn","Its perfect day at gym","My name is john","Its perfect day at gym")
nearest_3_row_id = c(3,8,4,8)
**df_1_out = data.frame(col_1,nearest_1,nearest_1_row_id,nearest_2,nearest_2_row_id,nearest_3,nearest_3_row_id)**
我试过了
library(stringdist)
df_1_out = df_1
df_1_out$nearest_1 = stringdist("My name is john","My name is jon", method = 'jw')
同样,我需要比较每一行。是否有任何替代方法来实现所需的输出。
【问题讨论】:
标签: r dplyr stringdist