如何在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

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

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