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,
“苹果”、“袜子”和“老鼠狗”。
我尝试这样做正则表达式_连接 and 模糊连接但字符串的顺序似乎很重要,并且似乎无法找到解决方法。
regex_left_join
有效,但它不仅仅是寻找任何相似之处。正如描述中所说,
通过 a 将表与字符串列连接起来另一个表中的正则表达式列
因此,我们需要提供一个正则表达式模式。如果df2$b
包含单独的感兴趣的单词,我们可以这样做
(df2$regex <- gsub(" ", "|", df2$b))
# [1] "Kiwi|Lemon|Apple" "Shirt|Sock|Glove" "Mouse|Dog"
and then
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
where -ncol(df1) - ncol(df2)
只需删除包含正则表达式模式的最后一列即可。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)