Introduction to Text Mining in R

· 4 min read · Updated March 16, 2026 · beginner
text-mining tidytext r nlp beginner

Text mining is the process of extracting meaningful patterns and insights from text data. Whether you’re analyzing customer reviews, social media posts, or academic papers, turning unstructured text into structured data opens up powerful analytical possibilities. This tutorial introduces you to text mining in R using the tidytext package—a modern approach that integrates seamlessly with the tidyverse.

Why Text Mining Matters

Traditional data analysis works with numbers and categories. But text data is everywhere: emails, survey responses, product reviews, news articles. Text mining lets you:

  • Quantify qualitative data: Convert opinions into scores
  • Discover themes: Identify topics across thousands of documents
  • Find patterns: Spot trends in customer feedback
  • Build features: Create variables for machine learning models

Installing Required Packages

You’ll need several packages for text mining in R:

# Install tidyverse (includes dplyr, tidyr, ggplot2)
install.packages("tidyverse")

# Install text mining packages
install.packages("tidytext")
install.packages("textdata")

# Install janeaustenr for practice data
install.packages("janeaustenr")

# Load them all
library(tidyverse)
library(tidytext)
library(janeaustenr)

The tidytext package by Julia Silge and David Robinson provides tools for converting text to and from tidy formats. The textdata package gives you access to sentiment lexicons.

The Tidy Text Format

The core principle of tidytext is simple: one token per row. A token is a meaningful unit of text—usually a word. This structure mirrors tidy data principles you’ve already seen in dplyr.

Consider this simple example:

# Start with a text vector
sample_text <- c(
  "Text mining is fascinating",
  "R makes text analysis easy",
  "Tidy data helps everywhere"
)

# Convert to tidy format (one word per row)
tidy_words <- tibble(text = sample_text) %>%
  unnest_tokens(word, text)

print(tidy_words)

The unnest_tokens() function does the heavy lifting—it tokenizes the text and creates a tidy data frame. The output shows each word on its own row with an index tracking which sentence it came from.

Tokenization: Breaking Text Apart

Tokenization is the first step in any text mining project. The unnest_tokens() function handles this intelligently:

# Create a sample tibble
text_df <- tibble(
  line = 1:3,
  text = c(
    "Machine learning is powerful",
    "Text mining reveals patterns",
    "R is the tool for data science"
  )
)

# Tokenize by words
word_tokens <- text_df %>%
  unnest_tokens(word, text)

print(word_tokens)

You can also tokenize by other units:

# Tokenize by sentences
sentence_tokens <- text_df %>%
  unnest_tokens(sentence, text, token = "sentences")

print(sentence_tokens)

# Tokenize by n-grams (pairs of consecutive words)
bigram_tokens <- text_df %>%
  unnest_tokens(bigram, text, token = "ngrams", n = 2)

print(bigram_tokens)

A Real Example: Jane Austen’s Novels

The janeaustenr package contains the complete texts of Jane Austen’s novels. Let’s analyze them:

# Get the book texts
austen_books <- janeaustenr::austen_books()

# Examine the structure
head(austen_books)

Each row contains a line from one of Austen’s six completed novels. Now let’s tidy this data:

# Tidy the novels
tidy_austen <- austen_books %>%
  # Group by book
  group_by(book) %>%
  # Tokenize into words
  unnest_tokens(word, text) %>%
  # Remove stop words (common words like "the", "is", "at")
  anti_join(get_stopwords())

print(tidy_austen)

The anti_join() with get_stopwords() removes common English words that don’t carry meaningful information.

Word Frequency Analysis

Once your text is tidy, analysis becomes straightforward:

# Find the most common words across all novels
word_counts <- tidy_austen %>%
  count(word, sort = TRUE)

print(word_counts)

This single pipeline counts word occurrences. Let’s visualize the top words:

# Plot top 15 words
word_counts %>%
  head(15) %>%
  ggplot(aes(n, fct_reorder(word, n))) +
  geom_col(fill = "steelblue") +
  labs(
    x = "Word Count",
    y = NULL,
    title = "Most Frequent Words in Jane Austen Novels"
  ) +
  theme_minimal()

Sentiment Analysis Basics

Sentiment analysis assigns emotional values to words. The tidytext package provides several sentiment lexicons:

# Load the Bing lexicon (positive/negative)
get_sentiments("bing") %>% head(10)

Let’s analyze sentiment in one of Austen’s books:

# Filter to Pride and Prejudice
pride_prejudice <- tidy_austen %>%
  filter(book == "Pride & Prejudice")

# Get sentiment for each word
sentiment_pride <- pride_prejudice %>%
  inner_join(get_sentiments("bing"))

# Count positive vs negative
sentiment_counts <- sentiment_pride %>%
  count(sentiment)

print(sentiment_counts)

Word Cloud Visualization

Word clouds provide an intuitive overview of term frequency:

library(wordcloud)

# Create word cloud of most common words
tidy_austen %>%
  count(word) %>%
  with(wordcloud(word, n, max.words = 50))

What You’ve Learned

This tutorial covered the fundamentals of text mining in R:

ConceptDescription
Tidy text formatOne token (typically word) per row
TokenizationBreaking text into meaningful units
Stop wordsCommon words to filter out
Word frequencyCounting and ranking terms
Sentiment analysisScoring words as positive/negative

The tidytext approach transforms messy text into data you can manipulate with dplyr, visualize with ggplot2, and model with R’s statistical tools.

See Also

  • dplyr::filter — Select rows by condition (used with sentiment analysis)
  • dplyr::count — Count observations by groups (essential for word frequency)
  • purrr::map — Apply functions to each element (useful for processing multiple texts)

Next Steps

Continue with the next tutorials in this series to deepen your text mining skills:

  • Tidytext Basics — Master the core tidytext functions for deeper text analysis
  • Sentiment Analysis in R — Explore advanced sentiment scoring techniques
  • Topic Modeling with LDA in R — Discover latent topics in large text corpora
  • Text Classification in R — Build machine learning models for text categorization

You’ll build on these foundations to analyze larger datasets, extract themes, and apply machine learning to text.