R/transform.R

Defines functions transform

# preprocessr
#
# Function: transform()
#
# Description: A function to transform data based
# on a variety of common transformations.
#
# Author: Peter Xenopoulos
#
#

transform <- function( data , type="center" , min = 0 , max = 0 , opts = 255 ) {

  # 'center' transformation substracts the data's mean
  if(type == "center")
    return( data - mean(data) )

  # 'resize' transformation subtracts an arbitrary number (default 255)
  if(type == "resize")
    return( data - opts )

  # 'scale' transformation divides by the data's sd
  if(type == "scale")
    return( data / sd(data) )

  # 'centerMed' transformation subtracts the data's median
  if(type == "centerMed")
    return( data - median(data) )

  # 'zscore' subtracts the mean and divides by the sd
  if(type == "zscore")
    return( ( data - mean(data) ) / sd(data) )

  # 'minimax' transforms data between 0 and 1
  if(type == "minmax")
    return( (data - min(data)) / (max(data) - min(data)) )

  # "decscale" transforms data between -1 and 1
  if(type == "decscale") {
    max.val = max( abs(data) )
    val = max.val
    c = 0
    while(val > 1) {
      val = val/(10^c)
      c = c + 1
    }
    return( data / 10^c )
  }

  # 'range' transformation transforms the data between a given range
  if(type == "range")
    return( ( (data-min(data))*(max-min) / (range(data)[2]-range(data)[1]) ) + min)

  # 'normalizer' normalizes the data using a norm. Use options from norm() function
  # default is using the one norm (maximum absolute column sum)
  if(type == "normalizer") {
    if( opts == 255 )
      opts = "O"
    data = as.matrix(data)
    return( data / norm(data,opts) )
  }

  # 'product' normalizes the data using a scalar product in opts
  if(type == "product")
    return( data * opts )
  
  # 'dst' transforms data according to the discrete cosine transform
  if(type == "dct")
    return( dct(data) )
  
  # 'dst' transforms data according to the discrete sine transform
  if(type == "dst")
    return( dst(data) )
  
  # 'dht' transforms data according to the discrete sine transform
  if(type == "dht")
    return( dht(data) )
  
  ### BELOW THIS LINE STILL IN DEV ###
  # 'bc' transformation performs a Box-Cox transformation

  # 'yj' transformation performs a Yeo-Johnson transformation

}
peterxeno/preprocessr documentation built on May 25, 2019, 2:10 a.m.