数据框定义
一种矩阵形式的数据,但数据框中各列可以是不同类型的数据。数据框每列是一个变量,每行是一个观测。数据框可以看成是矩阵的推广,也可看作一种特殊的列表对象。
数据框的创建
data.frame()
通过data.frame函数来创建数据框,该函数包含的主要参数为: data.frame(…, row.names = NULL, check.rows = FALSE, check.names = TRUE, stringsAsFactors = default.stringsAsFactors()) 如下所示:
|
|
rat.data
rats group sex glu.d1 glu.d7
1 1 postive F 11.97 12.97
2 2 postive M 12.97 13.97
3 3 model F 27.34 25.56
4 4 model M 23.97 23.97
5 5 control F 6.79 5.79
6 6 control M 5.19 6.19
注:在R中,当创建数据框的时候,它会将字符串类型的数据自动转换为因子型数据,此时需要添加stringsAsFactors = FALSE
即可。
expand.grid()
通过expand.grid()
创建一个数据框,此函数可以创建元素所有可能的组合:
|
|
x
h w sex
1 60 100 M
2 80 100 M
3 60 300 M
4 80 300 M
5 60 100 F
6 80 100 F
7 60 300 F
8 80 300 F
查看数据框信息
|
|
class(rat.data) # 查看类型
[1] “data.frame”
|
|
class(rat.data$sex) # 查看某列的类型
[1] “factor”
|
|
length(rat.data) # 查看数据框长度
[1] 5
|
|
names(rat.data) # 数据框各项名称
[1] “rats” “group” “sex” “glu.d1” “glu.d7”
|
|
summary(rat.data)
rats group sex glu.d1 glu.d7
Min. :1.00 control:2 F:3 Min. : 5.190 Min. : 5.790
1st Qu.:2.25 model :2 M:3 1st Qu.: 8.085 1st Qu.: 7.885
Median :3.50 postive:2 Median :12.470 Median :13.470
Mean :3.50 Mean :14.705 Mean :14.742
3rd Qu.:4.75 3rd Qu.:21.220 3rd Qu.:21.470
Max. :6.00 Max. :27.340 Max. :25.560
|
|
table(rat.data$group,rat.data$sex)
F M
control 1 1
model 1 1
postive 1 1
数据框的去重
|
|
unique(rat.data2) # 对数据框进行去重,即提取不重复的元素
rats group sex glu.d1 glu.d7
1 1 postive F 11.97 12.97
2 2 postive M 12.97 13.97
3 3 model F 27.34 25.56
4 4 model M 23.97 23.97
5 5 control F 6.79 5.79
6 6 control M 5.19 6.19
去重函数unique()
查询数据中的唯一值只可以使用unique()
函数,如下所示:
|
|
结果如下所示:
|
|
取重复的行duplicated()
查找重复数据的函数是duplicated()
,现在找出数据集iris
中的一个重复值,如下所示:
|
|
计算结果如下所示:
|
|
从上面结果可以看出来两个信息:
- 当duplicated()遇到重复的值时(重复的值必然至少有2个),第1个重复的值返回的是FALSE,到第2个以及第2个往后重复的值才显示为TRUE;
- duplicated()返回的是一个逻辑向量,因此可以将其当作索引去除重复行,如下所示:
|
|
计算结果如下所示:
|
|
再看一个案例:
|
|
rat.data2[duplicated(rat.data2),]# 取数据框中重复的行
rats group sex glu.d1 glu.d7
7 1 postive F 11.97 12.97
8 2 postive M 12.97 13.97
9 3 model F 27.34 25.56
10 4 model M 23.97 23.97
11 5 control F 6.79 5.79
12 6 control M 5.19 6.19
提取某个元素
rat.data[i,j]指第i行第j列的数据
|
|
rat.data[1,2] #取第1行第2列
[1] postive
Levels: control model postive
提取某行
|
|
rat.data[2,] #取第2行
rats group sex glu.d1 glu.d7
2 2 postive M 12.97 13.97rat.data[c(1,4),] #取1,4行
rats group sex glu.d1 glu.d7
1 1 postive F 11.97 12.97
4 4 model M 23.97 23.97
提取某列——用列号提取
|
|
rat.data[,3] #取第3列
[1] F M F M F M
Levels: F M
提取某列——用列名称提取
|
|
rat.data$rats #取rat.data数据框中的rats列
[1] 1 2 3 4 5 6
提取除掉某列的其余列
|
|
rat.data[,-3] #取除了第3列外的其余列
rats group glu.d1 glu.d7
1 1 postive 11.97 12.97
2 2 postive 12.97 13.97
3 3 model 27.34 25.56
4 4 model 23.97 23.97
5 5 control 6.79 5.79
6 6 control 5.19 6.19
取1到4行中的第4列
|
|
rat.data[1:4,4]
[1] 11.97 12.97 27.34 23.97
取1到4行中的第3列
|
|
rat.data[1:4,3]
[1] F M F M
Levels: F M
取第1行,2到4列
|
|
rat.data[1,2:4] #取第1行,2到4列
group sex glu.d1
1 postive F 11.97
提取满足某一条件的列==
或match()
提取rat.data中所有control级别的观察值
|
|
rat.data.control
rats group sex glu.d1 glu.d7
5 5 control F 6.79 5.79
6 6 control M 5.19 6.19
使用match()
函数,
在mtcars的行名称中检索Toyota Corolla
元素,如下所示:
|
|
计算结果如下所示:
|
|
使用with计算条件总和
案例:计算mtcars在不同马力段中油耗的平均值使用mean()
函数,在阈值为150马力的情况下,计算过程如下所示:
|
|
结果如下所示:
|
|
提取子集案例二
如下所示:
|
|
再看一案例,如下所示:
|
|
将某一数值转换为某一类别变量
创建数据框
|
|
leadership
manager date country gender age q1 q2 q3 q4 q5 stringsAsFactor
1 1 10/24/08 US M 32 5 4 5 5 5 FALSE
2 2 10/28/08 US F 45 3 5 2 5 5 FALSE
3 3 10/1/08 UK F 25 3 5 5 5 2 FALSE
4 4 10/12/08 UK M 39 3 3 4 NA NA FALSE
5 5 5/1/09 UK F 99 2 2 1 2 1 FALSE
转变变量(可转化为因子型变量)
要求:将大于99的转换为缺失,age大于75的为Elder,55到75之间的为Middle,55以下的为Young:
|
|
leadership
manager date country gender age q1 q2 q3 q4 q5 stringsAsFactor agecat
1 1 10/24/08 US M 32 5 4 5 5 5 FALSE Young
2 2 10/28/08 US F 45 3 5 2 5 5 FALSE Young
3 3 10/1/08 UK F 25 3 5 5 5 2 FALSE Young
4 4 10/12/08 UK M 39 3 3 4 NA NA FALSE Young
5 5 5/1/09 UK F NA 2 2 1 2 1 FALSE
判断数据框各行是否完整complete.cases
所用函数为complete.cases
|
|
complete.cases(rat.data1) #判断数据框是否完整
[1] TRUE FALSE TRUE TRUE TRUE TRUE
如果要删除某个数据集中的包含缺失值的行,那么就可以通过下面的代码实现:
|
|
提取非缺失值
|
|
rat.data1[complete.cases(rat.data1),] #选择非缺失值的数据
rats group sex glu.d1 glu.d7
1 1 postive F 11.97 12.97
3 3 model F 27.34 25.56
4 4 model M 23.97 23.97
5 5 control F 6.79 5.79
6 6 control M 5.19 6.19
增加列
直接添加
|
|
rat.data
rats group sex glu.d1 glu.d7 result
1 1 postive F 11.97 12.97 -1.00
2 2 postive M 12.97 13.97 -1.00
3 3 model F 27.34 25.56 1.78
4 4 model M 23.97 23.97 0.00
5 5 control F 6.79 5.79 1.00
6 6 control M 5.19 6.19 -1.00
cbind添加
格式为data.frame1 <- cbind(data.fram1,新的变量(可以由公式得到)),例如下面的例子就是在rat.data1添加一个result变量,result的变量是由rat.data1中的glu.d1减去glu.d7得到的。
|
|
rat.data1<-cbind(rat.data1, result=c(rat.data1$glu.d1-rat.data1$glu.d7))
rat.data1
rats group sex glu.d1 glu.d7 result
1 1 postive F 11.97 12.97 -1.00
2 2M 12.97 13.97 -1.00
3 3 model F 27.34 25.56 1.78
4 4 model M 23.97 23.97 0.00
5 5 control F 6.79 5.79 1.00
6 6 control M 5.19 6.19 -1.00
另外的方法为:
|
|
merge()添加
用法为total <- merge(dataframeA, dataframeB, by = "ID")
,它表示,将dataframeA与dataframeB按照ID进行合并,这种联结通常是横向的,即向数据框中添加变量。
删除列
使用NULL
|
|
rat.data1
rats group sex glu.d1 glu.d7 result
1 1 postive F 11.97 12.97 -1.00
2 2M 12.97 13.97 -1.00
3 3 model F 27.34 25.56 1.78
4 4 model M 23.97 23.97 0.00
5 5 control F 6.79 5.79 1.00
6 6 control M 5.19 6.19 -1.00rat.data1$result<-NULL # 删除result这一列
rat.data1
rats group sex glu.d1 glu.d7
1 1 postive F 11.97 12.97
2 2M 12.97 13.97
3 3 model F 27.34 25.56
4 4 model M 23.97 23.97
5 5 control F 6.79 5.79
6 6 control M 5.19 6.19
用subset()
|
|
rat.data1<-subset(rat.data1,select=c(-sex,-group)) #删除sex,group列
rat.data1
rats glu.d1 glu.d7
1 1 11.97 12.97
2 2 12.97 13.97
3 3 27.34 25.56
4 4 23.97 23.97
5 5 6.79 5.79
6 6 5.19 6.19
%in%
%in%
是一种特殊类型的函数,被称为二元运算符。
使用%in%
,如下所示:
先生成一个数据框,如下所示:
|
|
现在删除q3与q4,如下所示:
|
|
names(leadership)生成了一个包含所有变量名的字符型微量,即c("manager","date","country","gender","age","q1","q2","q3","q4","q5","stringsAsFactor")
,names(leadership)%in%c("item3","item4")
返回了一个逻辑型向量,names(leadership)``中每个匹配item3或item4的元素值为TRUE,反之为
FALSE,c(FALSE, FALSE, FALSE, FALSE,FALSE, FALSE, FALSE, TRUE, TRUE, FALSE),运算符非(!)将逻辑值反转:
c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE,FALSE, TRUE),
leadership[c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE,TRUE)]`选择了逻辑值为TRUE的列,于是q3和q4被剔除了。
或者直接使用-
号,如下所示:
|
|
增加行
|
|
rat.data1<-rbind(rat.data1,rat.data1)
rat.data1
rats glu.d1 glu.d7
1 1 11.97 12.97
2 2 12.97 13.97
3 3 27.34 25.56
4 4 23.97 23.97
5 5 6.79 5.79
6 6 5.19 6.19
7 1 11.97 12.97
8 2 12.97 13.97
9 3 27.34 25.56
10 4 23.97 23.97
11 5 6.79 5.79
12 6 5.19 6.19
删除行
|
|
rat.data1<-rat.data1[-8,] #删除第8行
rat.data1[-c(1,2),] #删除1,2行
rats glu.d1 glu.d7
3 3 27.34 25.56
4 4 23.97 23.97
5 5 6.79 5.79
6 6 5.19 6.19
7 1 11.97 12.97
9 3 27.34 25.56
10 4 23.97 23.97
11 5 6.79 5.79
12 6 5.19 6.19
数据框的排序
基于单列进行排序
排序所用的函数为sort()和order(),sort()是对元素直接进行排序,而order()则是反回排序后的元素在原来列中的位置,现在以内置的CO2数据为例说明,只取CO2的前10行:
|
|
raw_data <- CO2[1:10,]
raw_data
Plant Type Treatment conc uptake
1 Qn1 Quebec nonchilled 95 16.0
2 Qn1 Quebec nonchilled 175 30.4
3 Qn1 Quebec nonchilled 250 34.8
4 Qn1 Quebec nonchilled 350 37.2
5 Qn1 Quebec nonchilled 500 35.3
6 Qn1 Quebec nonchilled 675 39.2
7 Qn1 Quebec nonchilled 1000 39.7
8 Qn2 Quebec nonchilled 95 13.6
9 Qn2 Quebec nonchilled 175 27.3
10 Qn2 Quebec nonchilled 250 37.1
sort(raw_data$uptake) #对uptake进行排序,默认为升序
[1] 13.6 16.0 27.3 30.4 34.8 35.3 37.1 37.2 39.2 39.7
sort(raw_data$uptake,decreasing = TRUE) # decreasing为T时,为降序
[1] 39.7 39.2 37.2 37.1 35.3 34.8 30.4 27.3 16.0 13.6
order.raw_data <- order(raw_data$uptake);order.raw_data
[1] 8 1 9 2 3 5 10 4 6 7order的结果是说,在排序后的数据中,第1个元素位于原来的第8个位置
排序后的第1个数据是13.6,而13.6在原来uptake中位于第8
raw_data$uptake[order.raw_data] #这个结果与sort(raw_data$uptake)是一样的
[1] 13.6 16.0 27.3 30.4 34.8 35.3 37.1 37.2 39.2 39.7
raw_data[order.raw_data,] #按照uptake的升序对数据框进行排序
Plant Type Treatment conc uptake
8 Qn2 Quebec nonchilled 95 13.6
1 Qn1 Quebec nonchilled 95 16.0
9 Qn2 Quebec nonchilled 175 27.3
2 Qn1 Quebec nonchilled 175 30.4
3 Qn1 Quebec nonchilled 250 34.8
5 Qn1 Quebec nonchilled 500 35.3
10 Qn2 Quebec nonchilled 250 37.1
4 Qn1 Quebec nonchilled 350 37.2
6 Qn1 Quebec nonchilled 675 39.2
7 Qn1 Quebec nonchilled 1000 39.7
基于多列的排序
还以前述的数据为例说明:
|
|
order.raw_data2 <- with(raw_data,order(conc,uptake))
raw_data[order.raw_data2,]
Plant Type Treatment conc uptake
8 Qn2 Quebec nonchilled 95 13.6
1 Qn1 Quebec nonchilled 95 16.0
9 Qn2 Quebec nonchilled 175 27.3
2 Qn1 Quebec nonchilled 175 30.4
3 Qn1 Quebec nonchilled 250 34.8
10 Qn2 Quebec nonchilled 250 37.1
4 Qn1 Quebec nonchilled 350 37.2
5 Qn1 Quebec nonchilled 500 35.3
6 Qn1 Quebec nonchilled 675 39.2
7 Qn1 Quebec nonchilled 1000 39.7order.raw_data3 <- with(raw_data,order(uptake,conc))
raw_data[order.raw_data3,]
Plant Type Treatment conc uptake
8 Qn2 Quebec nonchilled 95 13.6
1 Qn1 Quebec nonchilled 95 16.0
9 Qn2 Quebec nonchilled 175 27.3
2 Qn1 Quebec nonchilled 175 30.4
3 Qn1 Quebec nonchilled 250 34.8
5 Qn1 Quebec nonchilled 500 35.3
10 Qn2 Quebec nonchilled 250 37.1
4 Qn1 Quebec nonchilled 350 37.2
6 Qn1 Quebec nonchilled 675 39.2
7 Qn1 Quebec nonchilled 1000 39.7
可以看出来,在第1种排序中, with(raw_data,order(conc,uptake)),先以order进行排序,如果order相同,则按照uptake进行排序;在第2种排序中,with(raw_data,order(uptake,conc)),先以uptake进行排序,如果uptake相同,则按conc进行排序。
混合排序
混合排序是指某一列是升序,某一列为降序,所用到的函数是xtfrm(),其中在需要降序的列前加上铅,在升序的列前加正号,现在以raw_data数据为例说明,以conc的降序,uptake的升序进行排序,用法如下:
|
|
order.raw_data4 <- order(-xtfrm(raw_data$conc),+raw_data$uptake)
raw_data[order.raw_data4,]
Plant Type Treatment conc uptake
7 Qn1 Quebec nonchilled 1000 39.7
6 Qn1 Quebec nonchilled 675 39.2
5 Qn1 Quebec nonchilled 500 35.3
4 Qn1 Quebec nonchilled 350 37.2
3 Qn1 Quebec nonchilled 250 34.8
10 Qn2 Quebec nonchilled 250 37.1
9 Qn2 Quebec nonchilled 175 27.3
2 Qn1 Quebec nonchilled 175 30.4
8 Qn2 Quebec nonchilled 95 13.6
1 Qn1 Quebec nonchilled 95 16.0