如何在R中将一个单词列表(chr)与数据框中多个列的值进行比较,并在匹配时输出二进制响应

我想将words列中的每个单词与V1V576列中的值进行比较(逐行对每一行)。如果words列中的任何单词与V列中的任何单词匹配,则用1替换相应V列中的单词;如果没有匹配,则用0替换。我知道如何做到这一点吗?我不确定如何对所有行和列进行循环

数据框名为Datawords列是一个列表($ words :List of 42201)。有42201行,大约有576列的单词需要比较(V1到V576)。

这是前3行和前20列的dput文件。

structure(list(id = c("Te-1", "Te-2", "Te-3"), category = c("Fabric Care", "Fabric Care", "Home Care"), brand = c("Tide", "Tide", "Cascade"), sub_category = c("Laundry", "Laundry", "Auto Dishwashing"),     market = c("US", "US", "US"), review_title = c("the best in a very crowded market",     "first time", "i have been using another well known brand and did not expect    "    ), review_text = c("the best general wash detergent  convenient container that keeps the product driy ",     "this helped to clean our washing machine after getting it from someone else   this review was collected as part of a promotion  ",     "i have been using another well known brand and did not expect much difference  wow  was i ever mistaken  i will never go back "    ), review_rating = c(5L, 5L, 5L), words = list(c("the", "best",     "general", "wash", "deterg", "conveni", "contain", "that",     "keep", "the", "product", "driy"), c("this", "help", "to",     "clean", "our", "wash", "machin", "after", "get", "it", "from",     "someon", "els", "this", "review", "was", "collect", "as",     "part", "of", "a", "promot"), c("i", "have", "been", "use",     "anoth", "well", "known", "brand", "and", "did", "not", "expect",     "much", "differ", "wow", "was", "i", "ever", "mistaken",     "i", "will", "never", "go", "back")), V1 = c("absolut", "absolut",     "absolut"), V2 = c("action", "action", "action"), V3 = c("actionpac",     "actionpac", "actionpac"), V4 = c("actual", "actual", "actual"    ), V5 = c("addit", "addit", "addit"), V6 = c("adverti", "adverti",     "adverti"), V7 = c("afford", "afford", "afford"), V8 = c("agent",     "agent", "agent"), V9 = c("allerg", "allerg", "allerg"),     V10 = c("allergi", "allergi", "allergi"), V11 = c("alon",     "alon", "alon")), row.names = c(NA, -3L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000023d166a1ef0>)

请查看下面的数据框片段,以更好地理解我的问题

点击这里查看数据表

非常感谢你的帮助!


回答:

我已经创建了一个数据框

数据

data <- data.frame(words = c("the, best, general","i, have, been"), v1 = c("best","no"), v2 = c("have", "nothing"), stringsAsFactors = F)

使用for循环条件,我已经传递了函数grepl,只要匹配就显示1,否则显示0

for (i in 2: ncol(data)){  for (j in 1:nrow(data)){    x <- i    y <- data$words[j]    ab <- data [j,x]     abc <- grepl (ab , y)       data[j,i] <- ifelse (abc %in% "TRUE", 1, data[j,i])      }}

结果

print (data)        words       v1     v2the, best, general  1      0   i, have, been    0      0

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注