R语言笔记之数据类型3-数据框

数据框定义

一种矩阵形式的数据,但数据框中各列可以是不同类型的数据。数据框每列是一个变量,每行是一个观测。数据框可以看成是矩阵的推广,也可看作一种特殊的列表对象。

数据框的创建

data.frame()

通过data.frame函数来创建数据框,该函数包含的主要参数为: data.frame(…, row.names = NULL, check.rows = FALSE, check.names = TRUE, stringsAsFactors = default.stringsAsFactors()) 如下所示:

1
2
3
4
5
6
7
rats <- c(1:6)
group <- c("postive","postive","model","model","control","control")
sex <- c("F","M","F","M","F","M")
glu.d1 <- c(11.97,12.97,27.34,23.97,6.79,5.19)
glu.d7 <- c(12.97,13.97,25.56,23.97,5.79,6.19)
rat.data <- data.frame(rats,group,sex,glu.d1,glu.d7)
rat.data

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()创建一个数据框,此函数可以创建元素所有可能的组合:

1
2
x<-expand.grid(h=c(60,80),w=c(100,300),sex=c("M","F"))
x

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

查看数据框信息

1
class(rat.data) # 查看类型

class(rat.data) # 查看类型
[1] “data.frame”

1
class(rat.data$sex) # 查看某列的类型

class(rat.data$sex) # 查看某列的类型
[1] “factor”

1
length(rat.data) # 查看数据框长度

length(rat.data) # 查看数据框长度
[1] 5

1
names(rat.data) # 数据框各项名称

names(rat.data) # 数据框各项名称
[1] “rats” “group” “sex” “glu.d1” “glu.d7”

1
2
summary(rat.data) 
# 数据框信息总结,对字符类/因子类数据,给出相应的频数统计;给数值型数据给出5个主要的指标

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

1
2
table(rat.data$group,rat.data$sex) 
# 将数据框中的group与sex关联为一个表

table(rat.data$group,rat.data$sex)
F M
control 1 1
model 1 1
postive 1 1

数据框的去重

1
2
rat.data2 <- rbind(rat.data,rat.data) # 创建一个新数据框rat.data2,内容是rat.data的2倍
unique(rat.data2) # 对数据框进行去重,即提取不重复的元素

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()函数,如下所示:

1
2
3
mtcars$cyl
unique(mtcars$cyl)
levels(as.factor(mtcars$cyl)) # 采用levles()这种方式只是核对一下,这种方式不推荐

结果如下所示:

1
2
3
4
5
6
> mtcars$cyl
[1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
> unique(mtcars$cyl)
[1] 6 4 8
> levels(as.factor(mtcars$cyl))
[1] "4" "6" "8"

取重复的行duplicated()

查找重复数据的函数是duplicated(),现在找出数据集iris中的一个重复值,如下所示:

1
2
3
4
5
6
7
8
x <- c(1, 2, 3, 3, 5, 4, 4, 5)
duplicated(x)
# 有重复的值就会标为TRUE,没有重复的值就标为FALSE
dupes <- duplicated(iris)
head(dupes)
which(dupes)
iris[dupes,]

计算结果如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
> x <- c(1, 2, 3, 3, 5, 4, 4, 5)
> duplicated(x)
[1] FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE
> # 有重复的值就会标为TRUE,没有重复的值就标为FALSE
> dupes <- duplicated(iris)
> head(dupes)
[1] FALSE FALSE FALSE FALSE FALSE FALSE
> which(dupes)
[1] 143
> iris[dupes,]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
143 5.8 2.7 5.1 1.9 virginica

从上面结果可以看出来两个信息:

  1. 当duplicated()遇到重复的值时(重复的值必然至少有2个),第1个重复的值返回的是FALSE,到第2个以及第2个往后重复的值才显示为TRUE;
  2. duplicated()返回的是一个逻辑向量,因此可以将其当作索引去除重复行,如下所示:
1
2
head(iris[!dupes, ])
nrow(iris[!dupes, ])

计算结果如下所示:

1
2
3
4
5
6
7
8
9
10
> head(iris[!dupes, ])
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
> nrow(iris[!dupes, ])
[1] 149

再看一个案例:

1
rat.data2[duplicated(rat.data2),]# 取数据框中重复的行

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列的数据

1
rat.data[1,2#取第1行第2列

rat.data[1,2] #取第1行第2列
[1] postive
Levels: control model postive

提取某行

1
2
rat.data[2,] #取第2行
rat.data[c(1,4),] #取1,4行

rat.data[2,] #取第2行
rats group sex glu.d1 glu.d7
2 2 postive M 12.97 13.97

rat.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

提取某列——用列号提取

1
rat.data[,3] #取第3列

rat.data[,3] #取第3列
[1] F M F M F M
Levels: F M

提取某列——用列名称提取

1
rat.data$rats #取rat.data数据框中的rats列

rat.data$rats #取rat.data数据框中的rats列
[1] 1 2 3 4 5 6

提取除掉某列的其余列

1
rat.data[,-3] #取除了第3列外的其余列

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列

1
rat.data[1:4,4]

rat.data[1:4,4]
[1] 11.97 12.97 27.34 23.97

取1到4行中的第3列

1
rat.data[1:4,3]

rat.data[1:4,3]
[1] F M F M
Levels: F M

取第1行,2到4列

1
rat.data[1,2:4] #取第1行,2到4列

rat.data[1,2:4] #取第1行,2到4列
group sex glu.d1
1 postive F 11.97

提取满足某一条件的列==match()

提取rat.data中所有control级别的观察值

1
2
3
Sel <- rat.data$group == "control"
rat.data.control <- rat.data[Sel,]
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元素,如下所示:

1
2
3
index <- match("Toyota Corolla", rownames(mtcars))
index
mtcars[index, 1:4]

计算结果如下所示:

1
2
3
4
5
6
> index <- match("Toyota Corolla", rownames(mtcars))
> index
[1] 20
> mtcars[index, 1:4]
mpg cyl disp hp
Toyota Corolla 33.9 4 71.1 65

使用with计算条件总和

案例:计算mtcars在不同马力段中油耗的平均值使用mean()函数,在阈值为150马力的情况下,计算过程如下所示:

1
2
3
with(mtcars, mean(mpg))
with(mtcars, mean(mpg[hp < 150]))
with(mtcars, mean(mpg[hp >= 150]))

结果如下所示:

1
2
3
4
5
6
> with(mtcars, mean(mpg))
[1] 20.09062
> with(mtcars, mean(mpg[hp < 150]))
[1] 24.22353
> with(mtcars, mean(mpg[hp >= 150]))
[1] 15.40667

提取子集案例二

如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
> manager<-c(1,2,3,4,5)
> date<-c("10/24/08","10/28/08","10/01/08","10/12/08","05/01/09")
> country<-c("US","US","UK","China","UK")
> gender<-c("M","F","F","M","F")
> age<-c(32,45,25,23,99)
> q1<-c(5,3,3,2,1)
> q2<-c(4,5,4,4,5)
> q3<-c(2,4,3,4,3)
> q4<-c(5,3,3,NA,2)
> q5<-c(5,5,3,3,NA)
> leadership<-data.frame(manager,date,country,gender,age,q1,q2,q3,q4,q5,stringsAsFactors=FALSE)
> leadership
manager date country gender age q1 q2 q3 q4 q5
1 1 10/24/08 US M 32 5 4 2 5 5
2 2 10/28/08 US F 45 3 5 4 3 5
3 3 10/01/08 UK F 25 3 4 3 3 3
4 4 10/12/08 China M 23 2 4 4 NA 3
5 5 05/01/09 UK F 99 1 5 3 2 NA
> newdata <- leadership[, c(6:10)] #从leadership数据框中取第6行到第10行的变量,并且将其保存在newdata中,将行下标留空(,),表示取所有行
> newdata
q1 q2 q3 q4 q5
1 5 4 2 5 5
2 3 5 4 3 5
3 3 4 3 3 3
4 2 4 4 NA 3
5 1 5 3 2 NA

再看一案例,如下所示:

1
2
3
4
5
6
7
8
9
10
11
> leadership
manager date country gender age q1 q2 q3 q4 q5
1 1 10/24/08 US M 32 5 4 2 5 5
2 2 10/28/08 US F 45 3 5 4 3 5
3 3 10/01/08 UK F 25 3 4 3 3 3
4 4 10/12/08 China M 23 2 4 4 NA 3
5 5 05/01/09 UK F 99 1 5 3 2 NA
> newdata<- leadership[which(leadership$gender == "M" & leadership$age>30),] # 将leaderhip中30岁以上的男性选入newdata中
> newdata
manager date country gender age q1 q2 q3 q4 q5
1 1 10/24/08 US M 32 5 4 2 5 5

将某一数值转换为某一类别变量

创建数据框

1
2
3
4
5
6
7
8
9
10
11
12
13
manager<-c(1,2,3,4,5)
date<-c("10/24/08","10/28/08","10/1/08","10/12/08","5/1/09")
country<-c("US","US","UK","UK","UK")
gender<-c("M","F","F","M","F")
age<-c(32,45,25,39,99)
q1<-c(5,3,3,3,2)
q2<-c(4,5,5,3,2)
q3<-c(5,2,5,4,1)
q4<-c(5,5,5,NA,2)
q5<-c(5,5,2,NA,1)
leadership<-data.frame(manager,date,country,gender,age,q1,q2,q3,q4,q5,stringsAsFactor=FALSE)
#把字符转化为因子
leadership

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:

1
2
3
4
5
leadership$age[leadership$age == 99] <-NA # 年龄为99时,表示缺失
leadership$agecat[leadership$age >75] <- "Elder" # 当age大于75时,agecat为Elder
leadership$agecat[leadership$age >= 55 & leadership$age<= 75] <- "Middle" # 当55<= age <=75时,agecat为Middle
leadership$agecat[leadership$age < 55] <-"Young" #age<55时,agecat为Young
leadership

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

1
2
3
rat.data1<-rat.data
rat.data1$group[2]<-NA
complete.cases(rat.data1) #判断数据框是否完整

complete.cases(rat.data1) #判断数据框是否完整
[1] TRUE FALSE TRUE TRUE TRUE TRUE

如果要删除某个数据集中的包含缺失值的行,那么就可以通过下面的代码实现:

1
2
3
4
rat.data1 <- rat.data1[complete.cases(rat.data1),]
# 或者是使用na.omit()函数
rat.data1 <- na.omit(rat.data1)

提取非缺失值

1
rat.data1[complete.cases(rat.data1),] #选择非缺失值的数据

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

增加列

直接添加

1
2
rat.data$result<-c(rat.data$glu.d1-rat.data$glu.d7)
rat.data

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得到的。

1
2
rat.data1<-cbind(rat.data1, result=c(rat.data1$glu.d1-rat.data1$glu.d7))
rat.data1

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 2 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

另外的方法为:

1
2
3
rat.data1$result <- rat.data1$glu.d1-rat.data1$glu.d7
# 或
rat.data1 <- transform(rat.data1,result=glu.d1-glu.d7)

merge()添加

用法为total <- merge(dataframeA, dataframeB, by = "ID"),它表示,将dataframeA与dataframeB按照ID进行合并,这种联结通常是横向的,即向数据框中添加变量。

删除列

使用NULL

1
2
3
rat.data1
rat.data1$result<-NULL # 删除result这一列,原数列有2列result
rat.data1

rat.data1
rats group sex glu.d1 glu.d7 result
1 1 postive F 11.97 12.97 -1.00
2 2 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

rat.data1$result<-NULL # 删除result这一列

rat.data1
rats group sex glu.d1 glu.d7
1 1 postive F 11.97 12.97
2 2 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

用subset()

1
2
rat.data1<-subset(rat.data1,select=c(-sex,-group)) #删除sex,group列
rat.data1

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%,如下所示:

先生成一个数据框,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
> manager<-c(1,2,3,4,5)
> date<-c("10/24/08","10/28/08","10/01/08","10/12/08","05/01/09")
> country<-c("US","US","UK","China","UK")
> gender<-c("M","F","F","M","F")
> age<-c(32,45,25,23,99)
> q1<-c(5,3,3,2,1)
> q2<-c(4,5,4,4,5)
> q3<-c(2,4,3,4,3)
> q4<-c(5,3,3,NA,2)
> q5<-c(5,5,3,3,NA)
> leadership<-data.frame(manager,date,country,gender,age,q1,q2,q3,q4,q5,stringsAsFactors=FALSE)
> leadership
manager date country gender age q1 q2 q3 q4 q5
1 1 10/24/08 US M 32 5 4 2 5 5
2 2 10/28/08 US F 45 3 5 4 3 5
3 3 10/01/08 UK F 25 3 4 3 3 3
4 4 10/12/08 China M 23 2 4 4 NA 3
5 5 05/01/09 UK F 99 1 5 3 2 NA

现在删除q3与q4,如下所示:

1
2
3
4
5
6
7
8
9
> myvars <-names(leadership)%in%c("q3","q4")
> newdata<-leadership[!myvars]
> newdata
manager date country gender age q1 q2 q5
1 1 10/24/08 US M 32 5 4 5
2 2 10/28/08 US F 45 3 5 5
3 3 10/01/08 UK F 25 3 4 3
4 4 10/12/08 China M 23 2 4 3
5 5 05/01/09 UK F 99 1 5 NA

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被剔除了。

或者直接使用-号,如下所示:

1
2
3
4
5
6
7
8
> newdata <- leadership[c(-8,-9) ] #直接在变量前加减号
> newdata
manager date country gender age q1 q2 q5
1 1 10/24/08 US M 32 5 4 5
2 2 10/28/08 US F 45 3 5 5
3 3 10/01/08 UK F 25 3 4 3
4 4 10/12/08 China M 23 2 4 3
5 5 05/01/09 UK F 99 1 5 NA

增加行

1
2
rat.data1<-rbind(rat.data1,rat.data1)
rat.data1

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

删除行

1
2
rat.data1<-rat.data1[-8,] #删除第8行
rat.data1[-c(1,2),] #删除1,2行

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行:

1
2
3
4
5
6
7
8
9
raw_data <- CO2[1:10,]
raw_data
sort(raw_data$uptake) #对uptake进行排序,默认为升序
sort(raw_data$uptake,decreasing = TRUE# decreasing为T时,为降序
order.raw_data <- order(raw_data$uptake);order.raw_data
# order的结果是说,在排序后的数据中,第1个元素位于原来的第8个位置
# 排序后的第1个数据是13.6,而13.6在原来uptake中位于第8
raw_data$uptake[order.raw_data] #这个结果与sort(raw_data$uptake)是一样的
raw_data[order.raw_data,] #按照uptake的升序对数据框进行排序

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 7

order的结果是说,在排序后的数据中,第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

基于多列的排序

还以前述的数据为例说明:

1
2
3
4
5
order.raw_data2 <- with(raw_data,order(conc,uptake))
raw_data[order.raw_data2,]
order.raw_data3 <- with(raw_data,order(uptake,conc))
raw_data[order.raw_data3,]

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.7

order.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的升序进行排序,用法如下:

1
2
order.raw_data4 <- order(-xtfrm(raw_data$conc),+raw_data$uptake)
raw_data[order.raw_data4,]

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