R 语言简介和应用领域
R 语言是一种用于统计计算和图形表示的编程语言和环境。它广泛应用于数据分析、统计建模、机器学习、数据可视化等领域。R 语言的开源特性使其成为学术研究和商业分析中的热门选择。
安装 R 和 RStudio
安装 R
- 访问 R 官方网站。
- 下载适合你操作系统的 R 安装包。
- 按照安装向导完成安装。
安装 RStudio
- 访问 RStudio 官方网站。
- 下载适合你操作系统的 RStudio Desktop 安装包。
- 按照安装向导完成安装。
R 基本语法
注释
# 这是单行注释
打印输出
print("Hello, R!")
数据类型和结构
向量
vec <- c(1, 2, 3, 4, 5)
矩阵
mat <- matrix(1:9, nrow = 3)
数据框
df <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35)
)
列表
lst <- list(vec, mat, df)
变量赋值和基本运算
变量赋值
x <- 10
y <- 20
基本运算
sum <- x + y
product <- x * y
读取和写入 CSV 文件
读取 CSV 文件
data <- read.csv("data.csv")
写入 CSV 文件
write.csv(data, "output.csv", row.names = FALSE)
读取 Excel 文件
安装 readxl 包
install.packages("readxl")
读取 Excel 文件
library(readxl)
data <- read_excel("data.xlsx")
数据库连接 (SQL)
安装 RSQLite 包
install.packages("RSQLite")
连接数据库
library(RSQLite)
con <- dbConnect(SQLite(), "database.sqlite")
查询数据
result <- dbGetQuery(con, "SELECT * FROM table")
Web 数据抓取
安装 rvest 包
install.packages("rvest")
抓取网页数据
library(rvest)
url <- "https://example.com"
page <- read_html(url)
title <- page %>% html_node("title") %>% html_text()
数据清洗
删除缺失值
clean_data <- na.omit(data)
替换缺失值
data[is.na(data)] <- 0
缺失值处理
插值法
library(zoo)
data$column <- na.approx(data$column)
数据重塑 (reshape2 包)
安装 reshape2 包
install.packages("reshape2")
数据重塑
library(reshape2)
melted_data <- melt(data, id.vars = "ID")
数据合并和分组
合并数据
merged_data <- merge(data1, data2, by = "ID")
分组数据
grouped_data <- aggregate(data$Value, by = list(data$Group), FUN = mean)
基础图形函数
散点图
plot(data$X, data$Y)
直方图
hist(data$Value)
ggplot2 包详解
安装 ggplot2 包
install.packages("ggplot2")
创建图形
library(ggplot2)
ggplot(data, aes(x = X, y = Y)) + geom_point()
交互式图表 (plotly)
安装 plotly 包
install.packages("plotly")
创建交互式图表
library(plotly)
plot_ly(data, x = ~X, y = ~Y, type = "scatter", mode = "markers")
地理数据可视化
安装 leaflet 包
install.packages("leaflet")
创建地图
library(leaflet)
leaflet() %>% addTiles() %>% addMarkers(lng = data$Longitude, lat = data$Latitude)
描述性统计
计算均值
mean_value <- mean(data$Value)
计算标准差
sd_value <- sd(data$Value)
假设检验
t 检验
t_test_result <- t.test(data$Group1, data$Group2)
相关分析
计算相关系数
correlation <- cor(data$X, data$Y)
回归分析
线性回归
model <- lm(Y ~ X, data = data)
summary(model)
方差分析 (ANOVA)
单因素方差分析
anova_result <- aov(Value ~ Group, data = data)
summary(anova_result)
主成分分析 (PCA)
进行 PCA
pca_result <- prcomp(data[, -1], scale. = TRUE)
summary(pca_result)
聚类分析
K-means 聚类
kmeans_result <- kmeans(data[, -1], centers = 3)
时间序列分析
创建时间序列对象
ts_data <- ts(data$Value, start = c(2020, 1), frequency = 12)
分解时间序列
decomposed_data <- decompose(ts_data)
监督学习 (分类, 回归)
逻辑回归
logistic_model <- glm(Outcome ~ Predictor, data = data, family = binomial)
非监督学习 (聚类, 降维)
K-means 聚类
kmeans_result <- kmeans(data[, -1], centers = 3)
模型评估和选择
交叉验证
library(caret)
train_control <- trainControl(method = "cv", number = 10)
model <- train(Outcome ~ Predictor, data = data, method = "glm", trControl = train_control)
文本预处理
安装 tm 包
install.packages("tm")
创建语料库
library(tm)
corpus <- Corpus(VectorSource(data$Text))
文本清理
corpus <- tm_map(corpus, content_transformer(tolower))
corpus <- tm_map(corpus, removePunctuation)
词频分析
创建文档-词项矩阵
dtm <- DocumentTermMatrix(corpus)
计算词频
freq <- colSums(as.matrix(dtm))
情感分析
安装 syuzhet 包
install.packages("syuzhet")
进行情感分析
library(syuzhet)
sentiment <- get_sentiment(data$Text)
主题模型
安装 topicmodels 包
install.packages("topicmodels")
进行 LDA 主题模型
library(topicmodels)
lda_model <- LDA(dtm, k = 3)
data.table 包
安装 data.table 包
install.packages("data.table")
创建 data.table
library(data.table)
dt <- data.table(data)
快速分组
grouped_dt <- dt[, .(mean_value = mean(Value)), by = Group]
dplyr 和 tidyr 包
安装 dplyr 和 tidyr 包
install.packages("dplyr")
install.packages("tidyr")
使用 dplyr 进行数据操作
library(dplyr)
filtered_data <- data %>% filter(Value > 10)
使用 tidyr 进行数据重塑
library(tidyr)
gathered_data <- data %>% gather(key = "Variable", value = "Value", -ID)
并行计算
安装 parallel 包
install.packages("parallel")
并行计算
library(parallel)
cl <- makeCluster(detectCores() - 1)
result <- parLapply(cl, data, function(x) x * 2)
stopCluster(cl)
大规模数据集处理策略
分块处理
chunk_size <- 1000
chunks <- split(data, rep(1:ceiling(nrow(data)/chunk_size), each = chunk_size, length.out = nrow(data)))
R Markdown 基础
创建 R Markdown 文档
install.packages("rmarkdown")
library(rmarkdown)
render("document.Rmd", output_format = "html_document")
创建动态报告
使用 R Markdown 创建报告
---
title: "Dynamic Report"
output: html_document
---
```{r}
summary(data)
## 交互式文档 (Shiny)
### 安装 `shiny` 包
```R
install.packages("shiny")
创建 Shiny 应用
library(shiny)
ui <- fluidPage(
titlePanel("Interactive Document"),
sidebarLayout(
sidebarPanel(
sliderInput("n", "Number of points:", min = 1, max = 100, value = 50)
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output) {
output$plot <- renderPlot({
plot(1:input$n, rnorm(input$n))
})
}
shinyApp(ui, server)
学术论文和演示文稿制作
使用 R Markdown 创建论文
---
title: "Academic Paper"
output: pdf_document
---
```{r}
summary(data)
## 函数定义和调用
### 定义函数
```R
my_function <- function(x) {
return(x * 2)
}
调用函数
result <- my_function(10)
匿名函数和闭包
匿名函数
result <- sapply(data, function(x) x * 2)
闭包
closure_function <- function(x) {
return(function(y) x + y)
}
add_five <- closure_function(5)
result <- add_five(10)
函数式编程概念
高阶函数
higher_order_function <- function(f, x) {
return(f(x))
}
result <- higher_order_function(function(x) x * 2, 10)
面向对象编程 (S3, S4 类)
S3 类
Person <- function(name, age) {
obj <- list(name = name, age = age)
class(obj) <- "Person"
return(obj)
}
print.Person <- function(obj) {
cat("Name:", obj$name, "\n")
cat("Age:", obj$age, "\n")
}
alice <- Person("Alice", 25)
print(alice)
S4 类
setClass("Person", slots = list(name = "character", age = "numeric"))
alice <- new("Person", name = "Alice", age = 25)
创建 R 包
创建包结构
package.skeleton("mypackage")
编写函数
# mypackage/R/my_function.R
my_function <- function(x) {
return(x * 2)
}
文档编写 (roxygen2)
安装 roxygen2 包
install.packages("roxygen2")
编写文档
#' My Function
#'
#' This function multiplies the input by 2.
#'
#' @param x A numeric value.
#' @return The input multiplied by 2.
#' @examples
#' my_function(10)
#' @export
my_function <- function(x) {
return(x * 2)
}
单元测试 (testthat)
安装 testthat 包
install.packages("testthat")
编写测试
library(testthat)
test_that("my_function works", {
expect_equal(my_function(10), 20)
})
发布包到 CRAN
检查包
check("mypackage")
提交包
submit_cran("mypackage")
代码优化技巧
向量化操作
result <- data$Value * 2
使用 Rcpp 和 C++ 集成
library(Rcpp)
cppFunction('int add(int x, int y) { return x + y; }')
result <- add(10, 20)
并行计算 (parallel 包)
并行计算
library(parallel)
cl <- makeCluster(detectCores() - 1)
result <- parLapply(cl, data, function(x) x * 2)
stopCluster(cl)
igraph 包使用
安装 igraph 包
install.packages("igraph")
创建图
library(igraph)
g <- graph_from_literal(A - B, B - C, C - A)
plot(g)
社交网络分析
计算度中心性
degree(g)
网络可视化
绘制网络图
plot(g)
sp 和 sf 包
安装 sp 和 sf 包
install.packages("sp")
install.packages("sf")
创建空间数据
library(sp)
coords <- matrix(c(1, 2, 3, 4), ncol = 2)
sp_points <- SpatialPoints(coords)
空间数据处理
创建空间数据框
library(sf)
sf_points <- st_as_sf(sp_points)
空间统计
计算距离
distances <- st_distance(sf_points)
地图制作
绘制地图
plot(sf_points)
Bioconductor 项目介绍
安装 Bioconductor
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install()
基因表达分析
安装 limma 包
Bi