R: Regex_Join/Fuzzy_Join - 以不同的词序连接不精确的字符串
R: Regex_Join/Fuzzy_Join - Join Inexact Strings in Different Word Orders
df1
df2
df3
library(dplyr)
library(fuzzyjoin)
df1 <- tibble(a =c("Apple Pear Orange", "Sock Shoe Hat", "Cat Mouse Dog"))
df2 <- tibble(b =c("Kiwi Lemon Apple", "Shirt Sock Glove", "Mouse Dog"),
c = c("Fruit", "Clothes", "Animals"))
# Appends 'Animals'
df3 <- regex_left_join(df1,df2, c("a" = "b"))
# Appends Nothing
df3 <- stringdist_left_join(df1, df2, by = c("a" = "b"), max_dist = 3, method = "lcs")
我想使用字符串将 df2 的 c 列附加到 df1,
'Apple'、'Sock' 和 'Mouse Dog'。
我试着用 regex_join 和 fuzzyjoin 来做这个,但是字符串的顺序似乎很重要,而且不能'似乎找不到解决方法。
regex_left_join
有效,但它不只是在寻找任何相似之处。正如描述中所说,
Join a table with a string column by a regular expression column in another table
因此,我们需要提供一个正则表达式模式。如果 df2$b
包含单独的感兴趣的词,我们可能会
(df2$regex <- gsub(" ", "|", df2$b))
# [1] "Kiwi|Lemon|Apple" "Shirt|Sock|Glove" "Mouse|Dog"
然后
regex_left_join(df1, df2, by = c(a = "regex"))[-ncol(df1) - ncol(df2)]
# A tibble: 3 x 3
# a b c
# <chr> <chr> <chr>
# 1 Apple Pear Orange Kiwi Lemon Apple Fruit
# 2 Sock Shoe Hat Shirt Sock Glove Clothes
# 3 Cat Mouse Dog Mouse Dog Animals
其中 -ncol(df1) - ncol(df2)
只是删除包含正则表达式模式的最后一列。
df1
df2
df3
library(dplyr)
library(fuzzyjoin)
df1 <- tibble(a =c("Apple Pear Orange", "Sock Shoe Hat", "Cat Mouse Dog"))
df2 <- tibble(b =c("Kiwi Lemon Apple", "Shirt Sock Glove", "Mouse Dog"),
c = c("Fruit", "Clothes", "Animals"))
# Appends 'Animals'
df3 <- regex_left_join(df1,df2, c("a" = "b"))
# Appends Nothing
df3 <- stringdist_left_join(df1, df2, by = c("a" = "b"), max_dist = 3, method = "lcs")
我想使用字符串将 df2 的 c 列附加到 df1, 'Apple'、'Sock' 和 'Mouse Dog'。
我试着用 regex_join 和 fuzzyjoin 来做这个,但是字符串的顺序似乎很重要,而且不能'似乎找不到解决方法。
regex_left_join
有效,但它不只是在寻找任何相似之处。正如描述中所说,
Join a table with a string column by a regular expression column in another table
因此,我们需要提供一个正则表达式模式。如果 df2$b
包含单独的感兴趣的词,我们可能会
(df2$regex <- gsub(" ", "|", df2$b))
# [1] "Kiwi|Lemon|Apple" "Shirt|Sock|Glove" "Mouse|Dog"
然后
regex_left_join(df1, df2, by = c(a = "regex"))[-ncol(df1) - ncol(df2)]
# A tibble: 3 x 3
# a b c
# <chr> <chr> <chr>
# 1 Apple Pear Orange Kiwi Lemon Apple Fruit
# 2 Sock Shoe Hat Shirt Sock Glove Clothes
# 3 Cat Mouse Dog Mouse Dog Animals
其中 -ncol(df1) - ncol(df2)
只是删除包含正则表达式模式的最后一列。