Counting text length:

library("stringi")
## Warning: package 'stringi' was built under R version 4.1.2
text = "What is the length of this sentence?"
print(stri_length(text))
## [1] 36
text = "欢迎学习大数据"
print(stri_length(text))
## [1] 56
text = "@_@😀😃"
print(stri_length(text))
## [1] 27

Counting the number of words:

text = "Welcome to HKU!"
word_count = sapply(strsplit(text, " "), length)
print(word_count)
## [1] 3

Counting the number of sentences:

text <- 'Hello world!! Here are two sentences for you...'
length(gregexpr('[[:alnum:] ][.!?]', text)[[1]])
## [1] 2

Sentiment Analysis:

library("syuzhet")
## Warning: package 'syuzhet' was built under R version 4.1.2
text = "HKU is a fantastic school, I love it."
syuzhet_vector <- get_sentiment(text, method="syuzhet")
head(syuzhet_vector)
## [1] 1.5
text = "HKU is a nice school and I like it."
bing_vector <- get_sentiment(text, method="bing")
head(bing_vector)
## [1] 2
text = "HKU is a nice school and I like it."
afinn_vector <- get_sentiment(text, method="afinn")
head(afinn_vector)
## [1] 5

Extracting emotions:

text = "HKU is a terrible school."
print(get_nrc_sentiment(text))
##   anger anticipation disgust fear joy sadness surprise trust negative positive
## 1     1            0       1    1   0       1        0     1        1        0

The Sentimentr package:

library("sentimentr")
## Warning: package 'sentimentr' was built under R version 4.1.2
## 
## Attaching package: 'sentimentr'
## The following object is masked from 'package:syuzhet':
## 
##     get_sentences
text = 'I am good'
sentiment_by(text)
##    element_id word_count sd ave_sentiment
## 1:          1          3 NA     0.4330127