R/mathutils.r

Defines functions dominant in.range rescale

# Author: Jorrel Khalil S. Aunario, [email protected]
# Date :  21 February 2012
# Version 0.0.1
# Licence GPL v3

rescale <- function(x, oldmin, oldmax, newmin, newmax){
  return((x-oldmin)*(newmax-newmin)/(oldmax-oldmin) + newmin)
}


in.range <- function(x, minofrange, maxofrange, min.inc=TRUE, max.inc=TRUE){
  if(minofrange>maxofrange){
    temp <- minofrange
    minofrange <- maxofrange
    maxofrange <- temp
  }
  uop <- ifelse(max.inc, "<=", "<")
  lop <- ifelse(min.inc, ">=", ">")
  
  return(get(lop)(x,minofrange) & get(uop)(x, maxofrange))
  
}

dominant <- function(x, na.rm=TRUE){
  if (!is.numeric(x)) stop("x should be numeric.")
  if (na.rm==TRUE) x <- x[!is.na(x)]
  
  if (length(x)<1) result <- NA else {
    frq <- table(x)
    result <- as.numeric(names(frq)[which(frq==max(frq))[1]]) # in case of tie get the first value
  }
  return(result)
}

Try the genutils package in your browser

Any scripts or data that you put into this service are public.

genutils documentation built on May 31, 2017, 3:20 a.m.