Description Usage Arguments Value Chaining See Also Examples
Approximate the emotion of text by grouping variable(s). For a
full description of the emotion detection algorithm see
emotion
. See emotion
for more details about the algorithm, the emotion/valence shifter keys
that can be passed into the function, and other arguments that can be passed.
1 | emotion_by(text.var, by = NULL, group.names, ...)
|
text.var |
The text variable. Also takes a |
by |
The grouping variable(s). 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 emotion
sentence_id - The id number of the sentences within each element_id
word_count - Word count sum
med by grouping variable
emotion_type - Type designation from the emotion
column of the emotion_dt
table
emotion_count - The number of profanities used by grouping variable
sd - Standard deviation (sd
) of the sentence level emotion rate by grouping variable
ave_emotion - Emotion rate
See the sentiment_by
for details about sentimentr chaining.
Other emotion functions:
emotion()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | ## Not run:
mytext <- c(
"I am not afraid of you",
NA,
"",
"I love it [not really]",
"I'm not angry with you",
"I hate it when you lie to me. It's so humiliating",
"I'm not happpy anymore. It's time to end it",
"She's a darn good friend to me",
"I went to the terrible store",
"There is hate and love in each of us",
"I'm no longer angry! I'm really experiencing peace but not true joy.",
paste("Out of the night that covers me, Black as the Pit from pole to",
"pole, I thank whatever gods may be For my unconquerable soul.",
"In the fell clutch of circumstance I have not winced nor cried",
"aloud. Under the bludgeonings of chance My head is bloody, but unbowed.",
"Beyond this place of wrath and tears Looms but the Horror of the",
"shade, And yet the menace of the years Finds, and shall find, me unafraid.",
"It matters not how strait the gate, How charged with punishments",
"the scroll, I am the master of my fate: I am the captain of my soul."
)
)
## works on a character vector but not the preferred method avoiding the
## repeated cost of doing sentence boundary disambiguation every time
## `emotion` is run
emotion(mytext)
emotion_by(mytext)
## preferred method avoiding paying the cost
mytext <- get_sentences(mytext)
emotion_by(mytext)
get_sentences(emotion_by(mytext))
(myemotion <- emotion_by(mytext))
stats::setNames(get_sentences(emotion_by(mytext)),
round(myemotion[["ave_emotion"]], 3))
pres <- get_sentences(presidential_debates_2012)
pres_emo_sent <- emotion_by(pres)
## method 1
pres_emo_per_time <- presidential_debates_2012 %>%
get_sentences() %>%
emotion_by(by = c('person', 'time'))
pres_emo_per_time
## method 2
library(magrittr)
presidential_debates_2012 %>%
get_sentences() %$%
emotion_by(., by = c('person', 'time'))
## method 3
presidential_debates_2012 %>%
get_sentences() %$%
emotion_by(dialogue, by = list(person, time))
## method 4
presidential_debates_2012 %>%
get_sentences() %>%
with(emotion_by(dialogue, by = list(person, time)))
plot(pres_emo_sent)
plot(pres_emo_per_time)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.