[英]sort data from one matrix by another matrix in R
我有點泡菜。 我有多個矩陣,想按前一個矩陣的最后一列對矩陣的第一列進行排序。 有任何想法嗎? 我嘗試過申請,訂購,排序等。
########################################################
######################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!!!!!
我想做的是由twoa [,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
目前尚不清楚您到底要做什么,但是如果您要查找每個矩陣的最后一列與下一個的第一列“匹配”,則可以像這樣組合order
和rank
:
onea<-one[order(one[,2]),];
twoa<-two[order(two[,1])[rank(onea[,2])],];
threea<-three[order(three[,1])[rank(twoa[,2])],]
foura<-four[order(four[,1])[rank(threea[,2])],]
fivea<-five[order(five[,1])[rank(foura[,2])],]
cbind(onea,twoa,threea,foura,fivea)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 2004 12345 12345 7777 7777 6789 6789 1113 1113 1212
## [2,] 2001 23456 23456 3456 3456 5677 5677 1111 1111 8888
## [3,] 2002 23567 23567 2345 2345 6678 6678 5555 5555 2222
## [4,] 2003 54321 54321 1234 1234 6767 6767 1112 1112 9999
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.