用R中的另一个矩阵从一个矩阵中排序数据

时间:2013-10-10 19:03:58

标签: r sorting matrix

我是一个泡菜。我有多个矩阵,并希望通过前一个矩阵的最后一列对矩阵的第一列进行排序。有任何想法吗?我尝试过申请,订购,排序等等。

  ########################################################
  ######################CODE#########################
  ### makes indiviual matrix of data ##
  ## this area would be replaced by read.files
  one<-matrix(c(2001,2002,2003,2004,23456, 23567,54321,12345),4,2);
  two<-matrix(c(54321,23567,23456,12345,1234,2345,3456,7777),4,2);
  three<-matrix(c(3456,7777,2345,1234,5677,6789,6678,6767),4,2);
  four<-matrix(c(6678,5677,6767,6789,5555,1111,1112,1113),4,2);
  five<-matrix(c(5555,1113,1112,1111,2222,1212,9999,8888),4,2);

  #######order all data######################
  onea<-one[order(one[,2]),];
  twoa<-two[order(two[,1]),];
  threea<-three[order(twoa[,2]),]; ####CANT GET THIS PART TO WORK!!!!!

我想要做的是按两个[,2]

排序
  ####output looking for: 
 cbind(onea,twoa,threea)
 [,1]  [,2]  [,3] [,4] [,5] [,6]
 [1,] 2004 12345 12345 7777 7777 6789
 [2,] 2001 23456 23456 3456 3456 5677
 [3,] 2002 23567 23567 2345 2345 6678
 [4,] 2003 54321 54321 1234 1234 6767

1 个答案:

答案 0 :(得分:1)

目前还不清楚你究竟在追求什么,但如果你正在寻找每个矩阵的最后一列与下一个矩阵的第一列“匹配”,你可以将order和{{1}结合起来像这样:

rank