Rutils/maybe-not-useful/boolean.utils.r

#==========================================================================================#
#==========================================================================================#
#     This function returns all possible combinations.                                     #
#------------------------------------------------------------------------------------------#
combn.boolean <<- function(x){
   #----- Stop if x isn't a vector. -------------------------------------------------------#
   if (! is.vector(x)) stop("x must be a vector")
   #---------------------------------------------------------------------------------------#

   #----- Get the length of vectors. ------------------------------------------------------#
   nx = length(x)
   #---------------------------------------------------------------------------------------#


   #----- Build the list with all possible combinations (except all negative). ------------#
   ans = mapply( FUN      = combn
               , m        = sequence(nx)
               , MoreArgs = list( x        = sequence(nx)
                                , FUN      = which.2.boolean
                                , along    = x
                                , simplify = TRUE
                                )#end list
               , SIMPLIFY = FALSE
               )#end mapply
   ans = lapply( X = ans, FUN = t)
   #---------------------------------------------------------------------------------------#

   #----- Concatenate all combinations to a matrix, and append the all FALSE case. --------#
   ans = do.call("rbind",ans)
   ans = rbind(rep(x=FALSE,times=nx),ans)
   #---------------------------------------------------------------------------------------#

   #----- Name the columns after the original x elements. ---------------------------------#
   colnames(ans) = x
   #---------------------------------------------------------------------------------------#

   return(ans)
}#end combn.boolean
#==========================================================================================#
#==========================================================================================#





#==========================================================================================#
#==========================================================================================#
#     This function converts indices into a Boolean.                                       #
#------------------------------------------------------------------------------------------#
which.2.boolean <<- function(idx,along){
   ans      = rep(FALSE,times=length(along))
   ans[idx] = TRUE
   return(ans)
}#end which.2.sel
#==========================================================================================#
#==========================================================================================#
manfredo89/ED2io documentation built on May 21, 2019, 11:24 a.m.