⚡ 编程实验室🏗️ HTML🎨 CSS⚡ JavaScript🐍 Python🗄️ SQL☕ Java⚛️ React💚 Vue🟢 Node.js⚙️ C语言🐘 PHP🐹 Go🔷 TypeScript🐬 MySQL🔧 C++🎯 C#🦀 Rust🅱️ Bootstrap💡 jQuery🎸 Django🍃 MongoDB👗 Sass🎪 Kotlin📊 R语言📋 XML📊 Excel🐘 PostgreSQL🐳 Docker🅰️ Angular🎮 游戏🏠 网站首页

R文本挖掘:使用tidytext进行词频分析

学习使用tidytext包对文本进行分词、词频统计和可视化。 · 难度:入门 · +15XP

文本挖掘概述

文本挖掘从非结构化文本中提取有用信息。tidytext基于tidyverse原则,将文本转为整洁格式(每行一个词)。

1. 安装与加载

library(tidytext)
library(dplyr)
library(ggplot2)

2. 创建文本数据框

text_df <- data.frame(
  line = 1:3,
  text = c('I love R programming', 'R is great for data analysis', 'I enjoy learning R')
)

3. 分词(Tokenization)

tokens <- text_df %>%
  unnest_tokens(word, text)
print(tokens)

4. 词频统计

word_counts <- tokens %>%
  count(word, sort = TRUE)
print(word_counts)

5. 去除停用词

data(stop_words)
tokens_clean <- tokens %>%
  anti_join(stop_words, by = 'word')
word_counts_clean <- tokens_clean %>% count(word, sort = TRUE)

6. 词云可视化

library(wordcloud)
wordcloud(words = word_counts_clean$word, freq = word_counts_clean$n, min.freq = 1)
函数说明
unnest_tokens()分词
anti_join(stop_words)去除停用词
count()词频统计

练习提示:使用janeaustenr包中的《傲慢与偏见》文本,统计最常用的10个词(去除停用词后)。

Ctrl+Enter
🚀 升级VIP
解锁全部课程+AI助手

🏆 学习排行

加载中...

📊 统计

📖 71 篇
0 完成
🔥 0