R语言数据结构
以下是为代码添加的详细注释,按功能模块编号整理:
1. 向量基础操作
# 1.1 创建数值向量
a <- c(1, 2, 5)    # 使用 c() 函数组合数值
a                  # 输出: [1] 1 2 5
# 1.2 创建字符向量
b <- c("one", "two")  # 字符串需用引号包裹
b                    # 输出: [1] "one" "two"
# 1.3 向量索引操作
a <- c("k", "j", "h", "a", "c", "m")
a[c(1, 3, 5)]       # 提取第1、3、5个元素 → [1] "k" "h" "c"
a[2:6]              # 提取第2到第6个元素 → [1] "j" "h" "a" "c" "m"
2. 矩阵操作
# 2.1 创建矩阵(默认按列填充)
y <- matrix(1:20, nrow=5, ncol=4)  # 5行4列矩阵
y <- matrix(1:20, nrow=4, ncol=5)  # 4行5列矩阵
# 2.2 创建带行列名的矩阵
cells <- c(1, 26, 24, 68)
rnames <- c("R1", "R2")     # 行名
cnames <- c("C1", "C2")     # 列名
mymatrix <- matrix(cells,
                   nrow=2,
                   ncol=2,
                   byrow=TRUE,        # 按行填充数据
                   dimnames = list(rnames, cnames))  # 设置行列名
mymatrix
# 输出:
#    C1 C2
# R1  1 26
# R2 24 68
# 2.3 自动填充矩阵
x <- matrix(1:10, nrow=2)   # 创建2x5矩阵,元素按列填充
3. 数据框操作
# 3.1 创建数据框
patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")  # 字符型向量
status <- c("Poor", "Improved", "Excellent", "Poor")
patientdata <- data.frame(patientID, age, diabetes, status)
# 3.2 数据框索引
patientdata[1:2]            # 提取前两列(列1: patientID, 列2: age)
patientdata[c("diabetes", "status")]  # 按列名提取
# 3.3 使用 $ 访问列
patientdata$age    # 提取 age 列 → [1] 25 34 28 52
# 3.4 数据探索与可视化
summary(mtcars$mpg)  # 查看 mpg 列的统计摘要(均值、分位数等)
plot(mtcars$mpg, mtcars$disp)  # 绘制 mpg 与 disp 的散点图
plot(mtcars$mpg, mtcars$wt)    # 绘制 mpg 与 wt 的散点图
# 3.5 环境绑定(谨慎使用)
attach(mtcars)      # 绑定 mtcars 到搜索路径,可直接访问列名
summary(mpg)        # 输出 mpg 的统计摘要
detach(mtcars)      # 解除绑定
4. 因子变量(分类变量)
# 4.1 字符型向量转因子
diabetes <- c("Type1", "Type2", "Type1", "Type1")
diabetes <- factor(diabetes)  # 转换为因子变量
# 输出因子结构:Levels: Type1 Type2
# 4.2 有序因子
status <- factor(status,
                 order = TRUE,  # 声明有序因子
                 levels = c("Poor", "Improved", "Excellent"))  # 指定顺序
status  # 输出:Levels: Poor < Improved < Excellent
# 4.3 查看数据框结构
patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
diabetes <- factor(diabetes)
status <- factor(status, order = TRUE)
patientdata <- data.frame(patientID, age, diabetes, status)
str(patientdata)  # 查看数据框结构,显示各列类型
5. 列表操作
# 5.1 创建列表(可存储混合数据类型)
g <- "My First List"                # 字符串
h <- c(25, 26, 18, 39)             # 数值向量
j <- matrix(1:10, nrow=5)          # 矩阵
k <- c("one", "two", "three")      # 字符向量
mylist <- list(
  title = g,     # 命名元素:title
  ages = h,      # 命名元素:ages
  j,             # 未命名元素:矩阵
  k              # 未命名元素:字符向量
)
mylist  # 输出包含4个元素的列表
关键说明
- 
因子变量
- 用 
factor()将字符型变量转换为分类变量 order=TRUE定义有序因子,需指定levels参数控制顺序
 - 用 
 - 
数据框与矩阵的区别
- 矩阵:所有元素类型相同
 - 数据框:列可包含不同类型数据
 
 - 
列表的灵活性
- 可存储任意类型对象(向量、矩阵、数据框等)
 - 支持命名元素,通过 
$访问(如mylist$title) 
 - 
注意事项
attach()/detach()可能导致变量名冲突,建议使用with()替代- 矩阵填充默认按列,设置 
byrow=TRUE改为按行填充 
 
                    
                
                
            
        
浙公网安备 33010602011771号