r - 如何从带有文本和日期值的 .csv 文件中的列表中计算特定正面/负面词的频率?在 R 中
问题描述
我正在尝试从包含消息、特定用户和日期的文档中获取情绪。我已经清理了两个文档,以便其中包含的单词具有标准格式,然后我尝试计算它们,但我似乎能够单独计算它们(在定义单词之后),但不能使用列表字。
文件。raw 格式为:text,user_id, date,正/负列表格式为:id,word_cz,polarity
file.raw <- read.csv("/Users/tomas/Desktop/Repromeda - Repromeda 3.csv", stringsAsFactors = FALSE,)
positive <- read.csv("/Users/tomas/Desktop/positive.txt", stringsAsFactors = FALSE,)
negative <- read.csv("/Users/tomas/Desktop/negative.txt", stringsAsFactors = FALSE,)
我可以用函数计算特定的单词,比如“Okay”
getCount <- function(data,keywords)
{
wordcount <- str_count(file.raw&text, keywords)
return(data.frame(data,wordcount))
}
file.raw$count <- getCount(file.raw&text,"okay")
)但我似乎无法找到一种方法来使用单词列表来自动化这个过程
理想的结果会为每行的每个正数和负数添加一列
谢谢您的帮助
解决方案
这个怎么样?
library(stringr)
data <- "yes i had a great time yesterday having fun but your lame actions were disturbing, ok?"
positive <- c("yes" , "ok", "fun", "great")
negative <- c("lame" , "disturbing", "no")
sapply(positive, function(x) str_count(data,x))
sapply(negative, function(x) str_count(data,x))
推荐阅读
- javascript - disable_with 使用渲染 js 时无法按预期工作:控制器中的“window.location=some_path”
- css - 如何将SVG中的foreignObject元素带到顶层?
- active-directory - 我可以使用脚本将命令从客户端发送到活动目录吗?
- ios - iOS 13 - 如何登录应用内购买沙盒帐户?
- java - 如何检查加盐和散列密码的匹配
- asp.net-core - 你如何在 asp.net 核心中制作一个剃刀视图扩展方法?
- c++ - 'CPythonGraphic::SaveJPEG':无法将参数 2 从 'char *' 转换为 'LPBYTE'
- browser - 没有开发工具的控制台日志记录
- android - 我想用 gridlayoutmanager recyclerview 来做这个
- javascript - 如果找到具有正确属性的 obj,则 Js 返回 true