R/er.R

#' @name er
#' @description Global sorting of rows
#' @title entire-row sorting 
#' @param X a matrix: each row a subject, each column a time/location across subjects
#' @param orderVar a scalar; one of the values in X upon which the percentage of occurence will determine the ordering of the rows. 
#' Defaults to the minimum value of X
#' @param naLast defaults to false; should NA values be sorted to first position or last. Defaults to FALSE.
#' 
#' @export
er <- function(X,orderVar=min(X,na.rm=TRUE), naLast=FALSE){
  if (length(orderVar) == 1){
    perc <- apply( X, 1, function(W){ sum(W==orderVar)  })
    return(X[order(perc), ])
  }
  X[order(orderVar), ]
}
swihart/lasagnar documentation built on May 30, 2019, 9:39 p.m.