[英]Change value of words in bing lexicon
我正在使用 R Studio 分析一项调查。 我正在使用来自 tidytext package 的 Bing Sentiment 词典来执行此操作。
有些词对我的调查没有正确的含义,特别是“温柔”被编码为积极,但我的受访者将“温柔”表示为消极(疼痛)。 我知道如何从 bing tibble 中删除一个单词,然后添加一个新单词,但是我怎样才能简单地更改单词的含义呢?
例如:
structure(list(word = c("pain", "tender", "sensitive", "headaches",
"like", "anxiety"), sentiment = c("negative", "positive", "positive",
"negative", "positive", "negative"), n = c(351L, 305L, 279L,
220L, 200L, 196L)), row.names = c(NA, 6L), class = "data.frame")
我希望它看起来像:
structure(list(word = c("pain", "tender", "sensitive", "headaches",
"like", "anxiety"), sentiment = c("negative", "negative", "positive",
"negative", "positive", "negative"), n = c(351L, 305L, 279L,
220L, 200L, 196L)), row.names = c(NA, 6L), class = "data.frame")
谢谢!
跑线
df$sentiment <- ifelse(df$word == "tender", "positive", df$sentiment)
将有效地改变word
向量“温柔”的任何实例的sentiment
向量,使其显示为“积极”。 任何其他实例将保持原样。
请注意,如果您还想将其他词更改为积极的情绪,您可以执行以下操作:
df$sentiment <- ifelse(df$word %in% c("tender", "anotherword", "etc"), "positive", df$sentiment)
在tidyverse
(在其上构建tidytext
)中进行这种重新编码的方法通常是:
library(tidyverse)
df %>%
mutate(sentiment = case_when(
word == "tender" ~ "negative",
TRUE ~ sentiment # means leave if none of the conditions are met
))
#> word sentiment n
#> 1 pain negative 351
#> 2 tender negative 305
#> 3 sensitive positive 279
#> 4 headaches negative 220
#> 5 like positive 200
#> 6 anxiety negative 196
case_when
遵循与ifelse
相同的逻辑,但您可以根据需要评估任意数量的条件,非常适合重新编码多个值。 ~
的左侧评估一个条件,如果满足该条件,则右侧说明该值。 您可以设置默认值,如case_when
中的最后一行所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.