order()関数

> x <- c(9,9:1)
> y <- c(1,1,3:1,1:4,3)
> z <-c(2,1:9)
> mat<-matrix(c(x,y,z),nrow=length(x))
> mat
      [,1] [,2] [,3]
 [1,]    9    1    2
 [2,]    9    1    1
 [3,]    8    3    2
 [4,]    7    2    3
 [5,]    6    1    4
 [6,]    5    1    5
 [7,]    4    2    6
 [8,]    3    3    7
 [9,]    2    4    8
[10,]    1    3    9
> ordmat<-mat[order(mat[,3]),]
> ordmat
      [,1] [,2] [,3]
 [1,]    9    1    1
 [2,]    9    1    2
 [3,]    8    3    2
 [4,]    7    2    3
 [5,]    6    1    4
 [6,]    5    1    5
 [7,]    4    2    6
 [8,]    3    3    7
 [9,]    2    4    8
[10,]    1    3    9
> ordmat2<-mat[order(mat[,3],mat[,1]),]
> ordmat2
      [,1] [,2] [,3]
 [1,]    9    1    1
 [2,]    8    3    2
 [3,]    9    1    2
 [4,]    7    2    3
 [5,]    6    1    4
 [6,]    5    1    5
 [7,]    4    2    6
 [8,]    3    3    7
 [9,]    2    4    8
[10,]    1    3    9
  • データフレームもソートする(こちら)