Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Is there an R function to order a matrix using two or more columns?

$
0
0

I simulated a data of 3 columns

aa <- rep(seq(0,1,0.05), seq(21,1,-1))
  bb <- NA
  for(i in length(seq(0,1,0.05)):1){
    bb <- c(bb,rep(seq(0,1,0.05),len = i))
  }
  bb <- bb[-1]  
  cc <- 1-(aa+bb)
data <- cbind(aa,bb,cc) 

Then, in my problem, a row containing (0,0,1) is equal to a row containing (1,0,0) and (0,1,0). So I use this code below to organize my data

for(i in 1:dim(Dominance)[1]){
    Dominance[i,] <- Dominance[i, order(Dominance[i,], decreasing = FALSE)]
}

The problem is that when I try to order using this code bellow, they order the first column well, but not the second column.

Dominance[order(Dominance[,1],Dominance[,2],Dominance[,3]),]

I got this as a result

  [1,] 0.00 0.00 1.00
  [2,] 0.00 0.00 1.00
  [3,] 0.00 0.00 1.00
  [4,] 0.00 0.05 0.95
         ...
  [59,] 0.00 0.50 0.50
  [60,] 0.00 0.50 0.50
  [61,] 0.05 0.35 0.60
  [62,] 0.05 0.35 0.60
  [63,] 0.05 0.05 0.90
  [64,] 0.05 0.05 0.90

The problem starts on line 61, once I have in the first column 0.05 and in the second column 0.35, but in the line 63 I have the same value in the first column (0.05) but in the second one I have a small value than 0.35.

Any ideas?

I have tried to use two other functions but they got the same results.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>