| sentiment_by {sentimentr} | R Documentation |
Approximate the sentiment (polarity) of text by grouping variable(s). For a
full description of the sentiment detection algorithm see
sentiment. See sentiment
for more details about the algorithm, the sentiment/valence shifter keys
that can be passed into the function, and other arguments that can be passed.
sentiment_by(text.var, by = NULL, averaging.function = sentimentr::average_downweighted_zero, group.names, ...)
text.var |
The text variable. Also takes a |
by |
The grouping variable(s). Default |
averaging.function |
A function for performing the group by averaging.
The default, |
group.names |
A vector of names that corresponds to group. Generally for internal use. |
... |
Other arguments passed to |
Returns a data.table with grouping variables plus:
element_id - The id number of the original vector passed to sentiment
sentence_id - The id number of the sentences within each element_id
word_count - Word count summed by grouping variable
sd - Standard deviation (sd) of the sentiment/polarity score by grouping variable
ave_sentiment - Sentiment/polarity score mean average by grouping variable
Other sentiment functions: sentiment
mytext <- c(
'do you like it? It is red. But I hate really bad dogs',
'I am the best friend.',
"Do you really like it? I'm not happy"
)
## works on a character vector but not the preferred method avoiding the
## repeated cost of doing sentence boundary disambiguation every time
## `sentiment` is run
## Not run:
sentiment(mytext)
sentiment_by(mytext)
## End(Not run)
## preferred method avoiding paying the cost
mytext <- get_sentences(mytext)
sentiment_by(mytext)
sentiment_by(mytext, averaging.function = average_mean)
sentiment_by(mytext, averaging.function = average_weighted_mixed_sentiment)
get_sentences(sentiment_by(mytext))
(mysentiment <- sentiment_by(mytext, question.weight = 0))
stats::setNames(get_sentences(sentiment_by(mytext, question.weight = 0)),
round(mysentiment[["ave_sentiment"]], 3))
pres_dat <- get_sentences(presidential_debates_2012)
## Not run:
## less optimized way
with(presidential_debates_2012, sentiment_by(dialogue, person))
## End(Not run)
## Not run:
sentiment_by(pres_dat, 'person')
(out <- sentiment_by(pres_dat, c('person', 'time')))
plot(out)
plot(uncombine(out))
sentiment_by(out, presidential_debates_2012$person)
with(presidential_debates_2012, sentiment_by(out, time))
highlight(with(presidential_debates_2012, sentiment_by(out, list(person, time))))
## End(Not run)
## Not run:
## tidy approach
library(dplyr)
library(magrittr)
cannon_reviews %>%
mutate(review_split = get_sentences(text)) %$%
sentiment_by(review_split)
## End(Not run)