scaleToUnit scales the columns of a numeric matrix to the unit range.

scaleToUnit(x, min = 0, max = 1, solveSingular = TRUE)

Arguments

x

a numeric matrix

min

the minimum in each dimension to scale to (defaults to 0)

max

the maximum in each dimension to scale to (defaults to 1)

solveSingular

if TRUE, constant columns will be transformed to the mean of min and max, not to NaN, as would result from a straighforward implementation

Value

Scaled x. The desired minimum and maximum values are returned as attributes min and max, respectively. The minimum/maximum values of x are returned as attributes min_x and max_x, respectively. The function used to transform a row in x to the desired range is returned as the tx attribute. The inverse transform can be find in the txInv attribute.

Examples

# scale the \code{iris} data set x <- iris[, 1:4] summary(x)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width #> Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 #> 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 #> Median :5.800 Median :3.000 Median :4.350 Median :1.300 #> Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199 #> 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800 #> Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
x2 <- scaleToUnit(x) summary(x2)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width #> Min. :0.0000 Min. :0.0000 Min. :0.0000 Min. :0.00000 #> 1st Qu.:0.2222 1st Qu.:0.3333 1st Qu.:0.1017 1st Qu.:0.08333 #> Median :0.4167 Median :0.4167 Median :0.5678 Median :0.50000 #> Mean :0.4287 Mean :0.4406 Mean :0.4675 Mean :0.45806 #> 3rd Qu.:0.5833 3rd Qu.:0.5417 3rd Qu.:0.6949 3rd Qu.:0.70833 #> Max. :1.0000 Max. :1.0000 Max. :1.0000 Max. :1.00000
# transform explicitly: x2[1, ]
#> Sepal.Length Sepal.Width Petal.Length Petal.Width #> 0.22222222 0.62500000 0.06779661 0.04166667
attr(x2, 'tx')(iris[1, 1:4])
#> Sepal.Length Sepal.Width Petal.Length Petal.Width #> 1 0.2222222 0.625 0.06779661 0.04166667
# inverse transform: y <- cbind(1:4,1) y
#> [,1] [,2] #> [1,] 1 1 #> [2,] 2 1 #> [3,] 3 1 #> [4,] 4 1
y2 <- scaleToUnit(y ,min = 0, max = 1) y2
#> [,1] [,2] #> [1,] 0.0000000 0.5 #> [2,] 0.3333333 0.5 #> [3,] 0.6666667 0.5 #> [4,] 1.0000000 0.5 #> attr(,"min_x") #> [1] 1 0 #> attr(,"max_x") #> [1] 4 2 #> attr(,"min") #> [1] 0 #> attr(,"max") #> [1] 1 #> attr(,"tx") #> function (x) #> txImpl(x, minX, maxX, min, max) #> <bytecode: 0x55dfb7dec150> #> <environment: 0x55dfb75b5640> #> attr(,"txInv") #> function (x) #> txInvImpl(x, minX, maxX, min, max) #> <bytecode: 0x55dfb7dec5b0> #> <environment: 0x55dfb75b5640>
y3 <- attr(y2, 'txInv')(y2) y3
#> [,1] [,2] #> [1,] 1 1 #> [2,] 2 1 #> [3,] 3 1 #> [4,] 4 1