From 474a8fc4f9dc6961f36bf3ad8c5ae5370847695a Mon Sep 17 00:00:00 2001 From: Dominic Ricottone Date: Sun, 3 Dec 2023 17:24:31 -0600 Subject: [PATCH] R demo --- r/Makefile | 11 +++++++++++ r/main.r | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 r/Makefile create mode 100644 r/main.r diff --git a/r/Makefile b/r/Makefile new file mode 100644 index 0000000..909b7a8 --- /dev/null +++ b/r/Makefile @@ -0,0 +1,11 @@ +.PHONY: install +install: + sudo pacman -S r-tidyr r-tidytext r-textdata + @echo "Interactively run R and call the following commands:" + @echo "> library(tidytext)" + @echo "> get_sentiments(\"afinn\")" + +.PHONY: run +run: + Rscript main.r + diff --git a/r/main.r b/r/main.r new file mode 100644 index 0000000..d0a855d --- /dev/null +++ b/r/main.r @@ -0,0 +1,26 @@ +#!/usr/bin/env Rscript + +library(tidytext) # for data, unnest_tokens +data(stop_words) + +library(janeaustenr) # for austen_books + +library(dplyr) # for group_by, anti_join, inner_join, count, mutate + +library(tidyr) # for pivot_wider + +tidy_books <- austen_books() %>% + group_by(book) %>% + unnest_tokens(word, text) %>% + anti_join(stop_words) + + +get_sentiments("afinn") + +sentiment <- tidy_books %>% + inner_join(get_sentiments("afinn")) %>% + summarise(sentiment = sum(value)) %>% + mutate(method = "AFINN") + +sentiment + -- 2.45.2