Search code examples
roptimizationconvex

I there a R function for finding a minimum between to pairs?


I need to identify pairs of variables that are dominated: if both values of the pair are lower than the other pairs in the data.

I already tried the functions min or pmin but I am not sure if they are the most appropriate.

   a = matrix(c(50,70), ncol = 2)
   b = matrix(c(45,85), ncol = 2)

  df = rbind(a,b)

Dominance <- function(a){
   for (i in 1:nrow(a)) {
      for (j in 1:nrow(a)) {
         i1 <- rowSums(a[i,] < a[j,]) == ncol(a)
         a[i1,, drop = FALSE]
      }
   } 
   return(a) 
}
l = Dominance(df)

> l
  X1 X2
1 45 65
2 50 70

I expect the pair (45,65) to be removed.


Solution

  • An option is to use do a comparison (<) between equal sized objects, then get the rowSums, if the sum is equal to the number of columns of the dataset, it implies all the elements in that row is less than the second data corresponding row

    f1 <- function(mat1, mat2) {
                i1 <- !rowSums(mat1 < mat2) == ncol(mat1)
                i2 <- !rowSums(mat2 < mat1) == ncol(mat2)
               rbind(mat1, mat2)[c(i1, i2),, drop = FALSE]
       }
    
    b <- matrix(c(45,65), ncol = 2)
    b1 <- matrix(c(45,85), ncol = 2)
    
    
    
    f1(a, b)
    #     [,1] [,2]
    #[1,]   50   70
    
    f1(a, b1)
    #     [,1] [,2]
    #[1,]   50   70
    #[2,]   45   85