R入门Day7:数据类型6---数据框

20200512·Kony·Win10
数据框

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

1.数据框的建立

data.frame()

data.frame(…, row.names = NULL, check.rows = FALSE, check.names = TRUE, stringsAsFactors = default.stringsAsFactors())
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
##    rats    group sex glu.d1 glu.d7
##  1    1  postive   F  11.97  12.97
##  2    2 positive   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

expand.grid()
此函数可以创建元素所有可能的组合

x<-expand.grid(h=c(60,80),w=c(100,300),sex=c("M","F"))
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

2. 查看数据框信息

class()

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

length()

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

name()

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

summary()
summary获取描述性统计量,可以提供最小值、最大值、四分位数和数值型变量的均值,以及因子向量和逻辑型向量的频数统计等

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)  # 将数据框中的group与sex关联为一个表
##            F M
##    control 1 1
##    model   1 1
##    postive 1 1

3. 数据框的去重

3.1 去重函数

查询数据中的唯一值只可以使用unique()函数

rat.data2 <- rbind(rat.data,rat.data)   # 创建一个新数据框rat.data2,内容是rat.data的2倍
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
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 

unique(mtcars$cyl)
##  [1] 6 4 8

levels(as.factor(mtcars$cyl))  # 采用levles()这种方式只是核对一下,这种方式不推荐
##  [1] "4" "6" "8"

3.2 取重复行

查找重复数据的函数是duplicated()

x <- c(1, 2, 3, 3, 5, 4, 4, 5, 3)
duplicated(x) b## 有重复的值就会标为TRUE,没有重复的值就标为FALSE
##  [1] FALSE FALSE FALSE  TRUE FALSE FALSE TRUE TRUE TRUE 
dupes <- duplicated(iris)  #iris是一个数值型数据框
head(dupes)
#[1] FALSE FALSE FALSE FALSE FALSE FALSE

which(dupes)
#[1] 143  ##第143个数据是第二次出现

iris[dupes,] ## 第143个数据的具体信息
#    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()返回的是一个逻辑向量,因此可以将其当作索引去除重复行,如下所示:
head(iris[!dupes, ])   ##前6行中没有重复的行
##    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
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

3.3 提取某个元素/行/列

rat.data[i,j]  #指第i行第j列的数据
rat.data[1,2] #取第1行第2列
##  [1] "postive"

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

rat.data[,2] # 取第二列的数据
##  [1] "postive"  "positive" "model"   "model"    "control"  "control" 

rat.data$group #第group列(第二列)的数据
## [1] "postive"  "positive" "model"   "model"    "control"  "control" 

rat.data[,-3] #取除了第3列外的其余列
##     rats   group glu.d1 glu.d7 
##  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

rat.data[1:4,4] #取1-4行的第4列的数据
##  [1] 11.97 12.97 27.34 23.97

3.4 提取满足某一条件的列==或函数match()

sel<-rat.data$group=="control"
rat.data.control<-rat.data[sel,]
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

sel
##  [1] FALSE FALSE FALSE FALSE  TRUE  TRUE
class(sel)
## [1] "logical"
index <- match("Toyota Corolla", rownames(mtcars))  ##在mtcars的行名称中检索Toyota Corolla元素
index
##  [1] 20

mtcars[index, 1:4]
##                  mpg cyl disp hp
##  Toyota Corolla 33.9   4 71.1 65

3.5 使用with()计算条件总和

with(mtcars, mean(mpg))  ## mtcars数据框中mpg的平均值
##  [1] 20.09062

with(mtcars, mean(mpg[hp < 150])) ## mtcars数据框中hp<150的mpg的平均值
##  [1] 24.22353

with(rat.data,mean(glu.d1)) ## rat.data数据框中,glu.d1的平均值
##  [1] 14.705

3.6 提取子集

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

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

## 1. 创建数据框
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
##     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
## 2. 转变变量(可转化为因子型变量)
## 将age大于99的转换为缺失,age大于75的为Elder,55到75之间的为Middle,55以下的为Young:
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
##    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   <NA>

4. 判断数据框各行是否完整

complete.cases

rat.data1<-rat.data
rat.data1$group[2]<-NA
##    rats   group sex glu.d1 glu.d7
##  1    1 postive   F  11.97  12.97
##  2    2    <NA>   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

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

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

rat.data1 <- rat.data1[complete.cases(rat.data1),]  

# 或者是使用na.omit()函数
rat.data1 <- na.omit(rat.data1)

5. 提取非缺失值(同4)

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

6. 增加列

直接增加

rat.data$result<-c(rat.data$glu.d1-rat.data$glu.d7)
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

rat.data$sum<-(rat.data$glu.d1+rat.data$glu.d7)
rat.data
##    rats    group sex glu.d1 glu.d7   sum
##  1    1  postive   F  11.97  12.97 24.94
##  2    2 positive   M  12.97  13.97 26.94
##  3    3    model   F  27.34  25.56 52.90
##  4    4    model   M  23.97  23.97 47.94
##  5    5  control   F   6.79   5.79 12.58
##  6    6  control   M   5.19   6.19 11.38

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
rat.data1<-cbind(rat.data1, result=c(rat.data1$glu.d1−rat.data1$glu.d7))

OR

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进行合并,这种联结通常是横向的,即向数据框中添加变量。

7. 删除列

使用NULL

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

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%是一种特殊类型的函数,被称为二元运算符

## 先生成一个数据框
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
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) ##了一个包含所有变量名的字符型微量
##   [1] "manager"         "date"           
##    [3] "country"         "gender"         
 ##   [5] "age"             "q1"             
##    [7] "q2"              "q3"             
##    [9] "q4"              "q5"             
##   [11] "strinsASFactors

或直接用-

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

8.增加行

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

9. 删除行

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

10. 数据框的排序

基于单列进行排序
排序所用的函数为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  7
# order的结果是说,在排序后的数据中,第1个元素位于原来的第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种排序中, with(raw_data,order(conc,uptake)),先以conc进行排序,如果order相同,则按照uptake进行排序;在第2种排序中,with(raw_data,order(uptake,conc)),先以uptake进行排序,如果uptake相同,则按conc进行排序。

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

r1=iris[order(iris[,1],iris[,3]),] ##第一列和第三列升序(第一列优先)
head(r1)
##     Sepal.Length Sepal.Width Petal.Length   Petal.Width Species
##  14          4.3         3.0          1.1           0.1  setosa
##  39          4.4         3.0          1.3           0.2  setosa
##  43          4.4         3.2          1.3           0.2  setosa
##  9           4.4         2.9          1.4           0.2  setosa
##  42          4.5         2.3          1.3           0.3  setosa
##  23          4.6         3.6          1.0           0.2  setosa

混和排序
混合排序是指某一列是升序,某一列为降序,所用到的函数是xtfrm(),其中在需要降序的列前加上减号,在升序的列前加正号

r2=iris[order(iris[,1],-iris[,3]),]
head(r2) ## 第一列升序,第三列降序(第一列优先)
##     Sepal.Length Sepal.Width Petal.Length   Petal.Width Species
##  14          4.3         3.0          1.1           0.1  setosa
##  9           4.4         2.9          1.4           0.2  setosa
##  39          4.4         3.0          1.3           0.2  setosa
##  43          4.4         3.2          1.3           0.2  setosa
##  42          4.5         2.3          1.3           0.3  setosa
##  4           4.6         3.1          1.5           0.2  setosa

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

使用dplyr的arrange

# 多条件排序:使用dplyr::arrange
library(dplyr)
data("iris")
head(iris)
##    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

# 第一列升序,然后是第三列升序
head(arrange(iris,iris[,1],iris[,3]))
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
##   1          4.3         3.0          1.1         0.1  setosa
##   2          4.4         3.0          1.3         0.2  setosa
##   3          4.4         3.2          1.3         0.2  setosa
##   4          4.4         2.9          1.4         0.2  setosa
##   5          4.5         2.3          1.3         0.3  setosa
##   6          4.6         3.6          1.0         0.2  setosa

# 第一列升序,然后是第三列降序
head(arrange(iris,iris[,1],-iris[,3]))
##     Sepal.Length Sepal.Width Petal.Length Petal.Width Species
##   1          4.3         3.0          1.1         0.1  setosa
##   2          4.4         2.9          1.4         0.2  setosa
##   3          4.4         3.0          1.3         0.2  setosa
##   4          4.4         3.2          1.3         0.2  setosa
##   5          4.5         2.3          1.3         0.3  setosa
##   6          4.6         3.1          1.5         0.2  setosa

# arrange可以直接输入列名,进行排序
head(arrange(iris,Sepal.Length, -Petal.Length))
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
##  1          4.3         3.0          1.1         0.1  setosa
##  2          4.4         2.9          1.4         0.2  setosa
##  3          4.4         3.0          1.3         0.2  setosa
##  4          4.4         3.2          1.3         0.2  setosa
##  5          4.5         2.3          1.3         0.3  setosa
##  6          4.6         3.1          1.5         0.2  setosa

笔记参考1:R语言基础--数据类型之数据框
笔记参考2:RVDSD
笔记参考3:R语言中 数据框排序 多条件排序

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,392评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,258评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,417评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,992评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,930评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,199评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,652评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,327评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,463评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,382评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,432评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,118评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,704评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,787评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,999评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,476评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,057评论 2 341

推荐阅读更多精彩内容