Learn R Programming

sentimentr (version 0.0.1)

sentiment_by: Polarity Score (Sentiment Analysis) By Groups

Description

Approximate the sentiment (polarity) of text by grouping variable(s).

Usage

sentiment_by(text.var, by = NULL, group.names, ...)

Arguments

text.var
The text variable.
by
The grouping variable(s). Default NULL uses the original row/element indices; if you used a column of 12 rows for text.var these 12 rows will be used as the grouping variable. Also takes a single grouping variable or a list of 1 or more grouping variables.
group.names
A vector of names that corresponds to group. Generally for internal use.
...
Other arguments passed to sentiment.

Value

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

See Also

Other sentiment.functions: sentiment

Examples

Run this code
mytext <- c(
   'do you like it?  But I hate really bad dogs',
   'I am the best friend.',
   'Do you really like it?  I\'m not happy'
)
sentiment(mytext)

sentiment_by(mytext)
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))

with(presidential_debates_2012, sentiment_by(dialogue, person))
(out <- with(presidential_debates_2012, sentiment_by(dialogue, list(person, time))))
plot(out)
plot(uncombine(out))

Run the code above in your browser using DataLab