Learn R Programming

wrMisc (version 2.1.0)

scaleXY: Rescale Data To Given Minimum- And Maxiumum- Values

Description

This function allows convenient rescaling of data in the sence that the range covered will be adjusted to user-specified values. Instead of fixed min and max values for the output it is also possible to fix 2 given quantiles for user-specified values (and rescale by linear transformation).

Usage

scaleXY(
  x,
  min = 0,
  max = NULL,
  q = c(0, 1),
  silent = FALSE,
  debug = FALSE,
  callFrom = NULL
)

Value

This function returns a vector (or matrix) of rescaled data (in dimensions of input)

Arguments

x

(numeric vector, matrix or data.frame) object to rescacle

min

(numeric) minimum value in output, if of length=2 and max not given/valid its 2nd value will be used as max

max

(numeric) maximum value in output, defaults to 1 if not specified or use 2nd value of min (if min of length=2)

q

(numeric, length=2) vector giving quantile values to be used for setting frame of normalization (defaults to lowest and highest values expected)

silent

(logical) suppress messages

debug

(logical) additional messages for debugging

callFrom

(character) allow easier tracking of messages produced

See Also

scale, normalizeThis, blockNormalize

Examples

Run this code
## simple vector
scale(11:19)    # standardizes (base R), is not 'rescaling'
scaleXY(11:19)  # range form 0 to 1
scaleXY(11:19, min=1, max=10)
scaleXY(11:19, c(1, 10))          # min & max as single argument
# rescale matrix with NA
## rescale to 20th and 80th quantile (ie other than min or max) 
scaleXY(11:19, min=1, max=10, q=c(0.2, 0.8))

## rescale matrix with NA
mat1 <- matrix(11:20, ncol=2, dimnames=list(letters[1:5], c("A","B")))
mat1[2,1] <- NA
scaleXY(mat1)
scaleXY(mat1, min=1, max=10, q=c(0.2, 0.8))

## rescale for each column individually
dat2 <- apply(mat1, 2, scaleXY, 1, 100)
range(dat2)
summary(dat2)
 

Run the code above in your browser using DataLab