数据框的下标与子集的提取
数据框的下标与子集的提取与矩阵基本相同. 不同的是: 对于列我们可以使用变量的名称, 仍以数据集Puromycin进行举例说明.
1、提取单个元素
> Puromycin[1, 1]
[1] 0.02
2、提取一个子集,例如第1, 3, 5行,第1, 3列
> Puromycin[c(1, 3, 5), c(1, 3)]
conc state
1 0.02 treated
3 0.06 treated
5 0.11 treated
> Puromycin[c(1, 3, 5), ]
conc rate state
1 0.02 76 treated
3 0.06 97 treated
5 0.11 123 treated
常使用变量名称来指定列的位置,上面的命令等价于
> Puromycin[c(1, 3, 5), c("conc", "state")]]
3、提取一列(变量的值). 一个数据框的变量对应了数据框的一列, 如果变量有名称, 则可直接使用“数据框名$变量名”这种格式指向对应的列. 例如
> Puromycin$conc # 等价于 Puromycin[,1]
[1] 0.02 0.02 0.06 0.06 0.11 0.11 0.22 0.22 0.56 0.56
[11] 1.10 1.10 0.02 0.02 0.06 0.06 0.11 0.11 0.22 0.22
[21] 0.56 0.56 1.10
> Puromycin$state
[1] treated treated treated treated treated
[6] treated treated treated treated treated
[11] treated treated untreated untreated untreated
[16] untreated untreated untreated untreated untreated
[21] untreated untreated untreated
Levels: treated untreated
4、提取满足条件的子集
> subset(Puromycin, state == "treated" & rate > 160)
conc rate state
9 0.56 191 treated
10 0.56 201 treated
11 1.10 207 treated
12 1.10 200 treated
> subset(Puromycin, conc > mean(conc))
conc rate state
9 0.56 191 treated
10 0.56 201 treated
11 1.10 207 treated
12 1.10 200 treated
21 0.56 144 untreated
22 0.56 158 untreated
23 1.10 160 untreated
下期请期待在数据框中添加新变量
暂无数据