gpt4 book ai didi

删除一列中的单词(如果存在于另一列中)

转载 作者:行者123 更新时间:2023-12-02 12:42:36 26 4
gpt4 key购买 nike

我有一个采用以下格式的数据框:

A <- c("John Smith", "Red Shirt", "Family values are better")
B <- c("John is a very highly smart guy", "We tried the tea but didn't enjoy it at all", "Family is very important as it gives you values")

df <- as.data.frame(A, B)

我的目的是将结果返回为:

ID   A                           B
1 John Smith is a very highly smart guy
2 Red Shirt We tried the tea but didn't enjoy it at all
3 Family values are better is very important as it gives you

我已经尝试过:

test<-df %>% filter(sapply(1:nrow(.), function(i) grepl(A[i], B[i])))

但这并没有得到我想要的输出。

最佳答案

一种解决方案是使用 mapply以及strsplit

诀窍是拆分df$A用单独的单词并折叠由 | 分隔的单词然后将其用作 patterngsub替换为 ""

lst <- strsplit(df$A, split = " ")

df$B <- mapply(function(x,y){gsub(paste0(x,collapse = "|"), "",df$B[y])},lst,1:length(lst))
df
# A B
# 1 John Smith is a very highly smart guy
# 2 Red Shirt We tried the tea but didn't enjoy it at all
# 3 Family values are better is very important as it gives you

另一个选项是:

df$B <- mapply(function(x,y)gsub(x,"",y) ,gsub(" ", "|",df$A),df$B)

数据:

A <- c("John Smith", "Red Shirt", "Family values are better")
B <- c("John is a very highly smart guy", "We tried the tea but didn't enjoy it at all", "Family is very important as it gives you values")

df <- data.frame(A, B, stringsAsFactors = FALSE)

关于删除一列中的单词(如果存在于另一列中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49949210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com