We can use Quarto with R!
library(tidyverse)
library(tidytext)
One way:
wtp1 = read_delim("pg67098.txt", delim = "\n", col_names = 'text')
wtp_words <- wtp1 %>%
mutate(linenumber = row_number()) %>%
unnest_tokens(word, text)
wtp_words <- wtp_words %>%
mutate(word = str_replace_all(word, "_", ""))
sentiments <- get_sentiments("afinn")
wtp_sentiments <- wtp_words %>%
inner_join(sentiments) %>%
mutate(index = linenumber %/% 80)
Joining, by = "word"
wtp_summarized_sentiment <- wtp_sentiments %>%
group_by(index) %>%
summarize(sentimentsum = sum(value), sentimentavg = mean(value))
ggplot(wtp_summarized_sentiment) + geom_col(aes(x=index, y=sentimentavg))
One way
write_delim(wtp1, file = "wtp_cleaned.txt", delim = "\n")
write_csv(wtp_summarized_sentiment, file = "sentiment_wtp.csv")