An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130615/e87234a3/attachment.pl>
Trouble sorting the matrix
3 messages · Ola Cabaj, andrija djurovic, Rui Barradas
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130615/18050b17/attachment.pl>
Hello, If I understand it correctly, the following should sort all columns of matrix3 and then unsort the sorted matrix. ord <- apply(matrix3, 2, order) # sort matrix3 mat4 <- sapply(seq_len(ncol(ord)), function(i) matrix3[ord[,i], i]) mat4 # unsort mat4 mat5 <- sapply(seq_len(ncol(ord)), function(i) mat4[order(ord[,i]), i]) identical(matrix3, mat5) # TRUE Hope this helps, Rui Barradas Em 15-06-2013 11:36, Ola Cabaj escreveu:
I would like to sort matrix3, so that every column in output matrix is
sorted. Also I have to "unsort" it later on.
matrix1<-matrix(rnorm(100,354,78),ncol=10)
matrix2<-matrix(rnorm(100,225,102),ncol=10)
matrix3<-cbind(matrix1,matrix2)
nrCol<-length(matrix3[1,])
class1<-1:10
for(i in 1:nrCol)
{
sorted.matrix<-matrix3[order(matrix3[class1,i]),]
}
for(i in 1:liczbaKolumn)
{
output<-sorted.matrix[order(matrix3[class1,i]),]
}