gpt4 book ai didi

r - 如何取一个词并根据该词在评论中的出现创建一个指示变量?

转载 作者:行者123 更新时间:2023-12-04 19:32:32 24 4
gpt4 key购买 nike

我有一个单词向量和一个评论向量:

word.list <- c("very", "experience", "glad")

comments <- c("very good experience. first time I have been and I would definitely come back.",
"glad I scheduled an appointment.",
"the staff have become more cordial.",
"the experience i had was not good at all.",
"i am very glad")

我想创建一个看起来像

的数据框
df <- data.frame(comments = c("very good experience. first time I have been and I would definitely come back.",
"glad I scheduled an appointment.",
"the staff have become more cordial.",
"the experience i had was not good at all.",
"i am very glad"),
very = c(1,0,0,0,1),
glad = c(0,1,0,0,1),
experience = c(1,0,0,1,0))

我有 12,000 多条评论和 20 个单词,我想用它来做这件事。我该如何有效地做到这一点?对于循环?还有其他方法吗?

最佳答案

一种方式是stringigdapTools包的组合,即

library(stringi)
library(qdapTools)

mtabulate(stri_extract_all(comments, regex = paste(word.list, collapse = '|')))
# experience glad very
#1 1 0 1
#2 0 1 0
#3 0 0 0
#4 1 0 0
#5 0 1 1

然后可以使用cbind或者data.frame来绑定(bind),

cbind(comments, mtabulate(stri_extract_all(comments, regex = paste(word.list, collapse = '|'))))) 

关于r - 如何取一个词并根据该词在评论中的出现创建一个指示变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43658614/

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