Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 为数据帧的每一行提取情感计算_R_Text Mining_Tidyr_Sentiment Analysis - Fatal编程技术网

R 为数据帧的每一行提取情感计算

R 为数据帧的每一行提取情感计算,r,text-mining,tidyr,sentiment-analysis,R,Text Mining,Tidyr,Sentiment Analysis,我有一个包含多行文本的数据框。我想为每行文本提取一个特定情感向量,该向量将是二进制的0不存在此情感或1存在。它们总共是5种情感,但我只想为似乎最重要的情感提取1 我尝试过的示例: library(tidytext) text = data.frame(id = c(11,12,13), text=c("bad movie","good movie","I think it would benefit religious people to see things like this, not ju

我有一个包含多行文本的数据框。我想为每行文本提取一个特定情感向量,该向量将是二进制的0不存在此情感或1存在。
它们总共是5种情感,但我只想为似乎最重要的情感提取1

我尝试过的示例:

library(tidytext)
text = data.frame(id = c(11,12,13), text=c("bad movie","good movie","I think it would benefit religious people to see things like this, not just to learn about our home, the Universe, in a fun and easy way, but also to understand that non- religious explanations don't leave people hopeless and",))
nrc_lexicon <- get_sentiments("nrc")
任何提示都会对我有帮助

下一步是什么?
我如何使用nrc词汇分析调用每一行

for (i in 1:nrow(text)) {
(text$text[i], nrc_lexicon)
}
那么这个呢:

library(tidytext)   # library for text
library(dplyr)

# your data
text <- data.frame(id = c(11,12,13),
 text=c("bad movie","good movie","I think it would benefit religious
 people to see things like this, not just to learn about our home, 
the Universe, in a fun and easy way, but also to understand that non- religious
 explanations don't leave people hopeless and"), stringsAsFactors = FALSE)  # here put this option, stringAsFactors = FALSE!

# the lexicon
nrc_lexicon <- get_sentiments("nrc")

# now the job
unnested <- text %>%
             unnest_tokens(word, text) %>%  # unnest the words
             left_join(nrc_lexicon) %>%     # join with the lexicon to have sentiments
             left_join(text)                # join with your data to have titles
如果您想将其作为
data.frame

 df_sentiment <- as.data.frame.matrix(table_sentiment)

df_情感1]输入和预期输出是same@akrun很抱歉,我更新了它。你是在问如何循环数据框并在每行应用词典吗?@ghub24在每行应用词典,并为存在的情感提取1most@Kkyr你的文本总是局限于“坏电影”“好电影”吗?你是否也在寻找如何在文本和情绪表中找到匹配项的答案?在这种情况下,您必须给出更多的示例,我假设您只是在寻找一种合并数据的方法
table_sentiment <- table(unnested$id, unnested$sentiment)
table_sentiment
     anger anticipation disgust fear joy negative positive sadness surprise trust
  11     1            0       1    1   0        1        0       1        0     0
  12     0            1       0    0   1        0        1       0        1     1
  13     0            1       0    1   1        2        3       2        1     0
 df_sentiment <- as.data.frame.matrix(table_sentiment)
df_sentiment[df_sentiment>1]<-1
df_sentiment
   anger anticipation disgust fear joy negative positive sadness surprise trust
11     1            0       1    1   0        1        0       1        0     0
12     0            1       0    0   1        0        1       0        1     1
13     0            1       0    1   1        1        1       1        1     0