[1] "numeric"
[1] "integer"
从入门到生物医学数据分析
| 技能 | 应用场景 |
|---|---|
| ✅ 数据类型与结构 | 存储患者信息、基因表达数据 |
| ✅ 向量化操作 | 批量处理基因组数据 |
| ✅ 控制流与函数 | 自动化分析流程 |
| ✅ R包管理 | 安装Bioconductor工具 |
R是用于统计计算和图形的编程语言与环境
| 特点 | 说明 | 生信应用 |
|---|---|---|
| 开源免费 | GPL许可证 | 学术界广泛使用 |
| 跨平台 | Win/Mac/Linux | 服务器/本地均可运行 |
| 交互式 | 即时反馈 | 快速探索数据 |
| 包生态 | 20,000+包 | Bioconductor生信专用 |
💡 建议:两者互补!R做统计可视化,Python做深度学习
https://posit.co/download/rstudio-desktop/
┌─────────────────┬─────────────────┐
│ Source │ Environment │
│ (代码编辑器) │ (变量查看) │
│ ├─────────────────┤
│ │ Files/Plots/ │
│ │ Packages/Help │
├─────────────────┴─────────────────┤
│ Console (控制台) │
│ 输入命令,查看输出 │
└───────────────────────────────────┘
| 类型 | 说明 | 示例 | 生信应用 |
|---|---|---|---|
numeric |
数值型 | 3.14 |
基因表达量 |
integer |
整数型 | 42L |
读数计数 |
character |
字符型 | "TP53" |
基因名 |
logical |
逻辑型 | TRUE |
差异表达标志 |
complex |
复数型 | 3+4i |
傅里叶变换 |
向量是R中最基本的数据结构,所有元素必须是同一类型
[1] 1 2 3 4 5 6 7 8 9 10
[1] 1 11 21 31 41 51 61 71 81 91
[1] "Tumor" "Tumor" "Tumor" "Normal" "Normal" "Normal"
[1] 13.7 20.1 15.8 21.7
[1] 17.0 24.6 13.4 30.4
[1] FALSE TRUE FALSE TRUE
[1] 10.675
[1] 3.814337
[1] 2
💡 最佳实践:用向量化代替循环,速度提升10-100倍!
S1 S2 S3 S4
TP53 8.5 12.3 6.7 15.2
KRAS 5.2 7.8 9.1 6.5
BRCA1 3.1 4.5 8.2 7.9
patient_id age gender tumor_stage tp53_expr
1 P001 45 M II 8.5
2 P002 52 F III 12.3
3 P003 38 F I 6.7
4 P004 61 M IV 15.2
5 P005 29 F II 9.1
[1] 8.5 12.3 6.7 15.2 9.1
[1] 45 52 38 61 29
age gender
1 45 M
2 52 F
3 38 F
4 61 M
5 29 F
patient_id age gender tumor_stage tp53_expr
2 P002 52 F III 12.3
4 P004 61 M IV 15.2
patient_id age gender tumor_stage tp53_expr
4 P004 61 M IV 15.2
patient_id age gender tumor_stage tp53_expr
2 P002 52 F III 12.3
4 P004 61 M IV 15.2
'data.frame': 5 obs. of 5 variables:
$ patient_id : chr "P001" "P002" "P003" "P004" ...
$ age : num 45 52 38 61 29
$ gender : chr "M" "F" "F" "M" ...
$ tumor_stage: chr "II" "III" "I" "IV" ...
$ tp53_expr : num 8.5 12.3 6.7 15.2 9.1
patient_id age gender tumor_stage
Length:5 Min. :29 Length:5 Length:5
Class :character 1st Qu.:38 Class :character Class :character
Mode :character Median :45 Mode :character Mode :character
Mean :45
3rd Qu.:52
Max. :61
tp53_expr
Min. : 6.70
1st Qu.: 8.50
Median : 9.10
Mean :10.36
3rd Qu.:12.30
Max. :15.20
patient_id age gender tumor_stage tp53_expr
1 P001 45 M II 8.5
2 P002 52 F III 12.3
3 P003 38 F I 6.7
[1] 5 5
[1] 5
[1] "patient_id" "age" "gender" "tumor_stage" "tp53_expr"
[1] "TP53 : 12"
[1] "KRAS : 13"
[1] "BRCA1 : 10"
calculate_fc <- function(tumor, normal, log2 = TRUE) {
# 计算均值
mean_tumor <- mean(tumor)
mean_normal <- mean(normal)
# 计算Fold Change
fc <- mean_tumor / mean_normal
if (log2) {
fc <- log2(fc)
}
return(fc)
}
# 使用示例
tumor_expr <- c(12.3, 15.2, 18.5, 14.1)
normal_expr <- c(6.7, 7.8, 8.2, 7.5)
calculate_fc(tumor_expr, normal_expr)[1] 0.9928164
# 差异表达分析函数
analyze_de <- function(expression,
group,
method = "t-test",
alpha = 0.05) {
# 分组
group1 <- expression[group == "Tumor"]
group2 <- expression[group == "Normal"]
# t检验
result <- t.test(group1, group2)
list(
p_value = result$p.value,
significant = result$p.value < alpha,
method = method
)
}
# 使用
expr <- c(12.3, 15.2, 18.5, 6.7, 7.8, 8.2)
group <- c("Tumor", "Tumor", "Tumor", "Normal", "Normal", "Normal")
analyze_de(expr, group)$p_value
[1] 0.0423099
$significant
[1] TRUE
$method
[1] "t-test"
| 仓库 | 定位 | 包数量 | 生信代表 |
|---|---|---|---|
| CRAN | 综合R包 | 20,000+ | ggplot2, dplyr |
| Bioconductor | 生物信息 | 2,000+ | DESeq2, Seurat |
| 包 | 功能 | 生信应用 |
|---|---|---|
dplyr |
数据处理 | 筛选基因、分组统计 |
ggplot2 |
可视化 | 火山图、热图 |
readr |
数据读取 | 读取表达矩阵 |
tidyr |
数据整理 | 长宽格式转换 |
| 包 | 功能 | 应用场景 |
|---|---|---|
DESeq2 |
RNA-seq分析 | 差异表达 |
Seurat |
单细胞分析 | 细胞聚类 |
GenomicRanges |
基因组区间 | 注释分析 |
分析5个肿瘤样本和5个正常样本的TP53基因表达数据
sample group expression
1 Tumor_1 Tumor 12.5
2 Tumor_2 Tumor 15.2
3 Tumor_3 Tumor 8.7
4 Tumor_4 Tumor 18.3
5 Tumor_5 Tumor 11.9
6 Normal_1 Normal 6.2
'data.frame': 10 obs. of 3 variables:
$ sample : chr "Tumor_1" "Tumor_2" "Tumor_3" "Tumor_4" ...
$ group : chr "Tumor" "Tumor" "Tumor" "Tumor" ...
$ expression: num 12.5 15.2 8.7 18.3 11.9 6.2 7.5 5.8 8.1 6.9
group expression
1 Normal 6.90
2 Tumor 13.32
[1] 0.01458753
| 指标 | 数值 | 解释 |
|---|---|---|
| 肿瘤组均值 | 13.32 | 高表达 |
| 正常组均值 | 6.90 | 低表达 |
| Fold Change | 1.93 | 上调近2倍 |
| p-value | < 0.05 | 显著差异 |
TP53在肿瘤组中显著上调,提示可能参与肿瘤发生发展
df$gene, df[condition, ]t.test(), mean(), cor()BiocManager::install()王诗翔 副教授
中南大学生物医学信息系
📧 wangshx@csu.edu.cn
🐙 https://github.com/WangLabCSU

R语言基础 | 中南大学