This will scale the numeric vector x linearly from an old scale between low and high to a new one between newlow and newhigh.

LinScale(x, low = NULL, high = NULL, newlow = 0, newhigh = 1)

Arguments

x

a numeric matrix(like object).

low

numeric. The minimum value of the scale, defaults to min(x). This is calculated columnwise by default; defined low or high arguments will be recycled if necessary.

high

numeric. The maximum value of the scale, defaults to max(x). This is calculated columnwise by default; when a maxval is entered, it will be recycled.

newlow

numeric. The minimum value of the new scale, defaults to 0, resulting in a 0-1 scale for x. newlow is recycled if necessary.

newhigh

numeric. The maximum value of the scale, defaults to 1. newhigh is recycled if necessary.

Details

Hmm, hardly worth coding...

Value

The centered and scaled matrix. The numeric centering and scalings used (if any) are returned as attributes "scaled:center" and "scaled:scale"

Author

Andri Signorell <andri@signorell.net>

See also

Examples

# transform the temperature from Celsius to Fahrenheit
LinScale(d.pizza[1:20, "temperature"], 0, 100, -17.8, 37.8 )
#>          [,1]
#>  [1,] 11.6680
#>  [2,] 13.5584
#>  [3,]  2.4940
#>  [4,]      NA
#>  [5,] 10.0000
#>  [6,] -2.7880
#>  [7,]  1.0484
#>  [8,] 12.6688
#>  [9,]  8.8880
#> [10,] 12.4464
#> [11,] -1.7872
#> [12,] 10.7228
#> [13,] -4.4282
#> [14,]  2.0492
#> [15,] 12.0016
#> [16,] 10.7228
#> [17,] 10.5560
#> [18,]  8.7212
#> [19,] 11.5568
#> [20,] -6.6800
#> attr(,"scaled:center")
#> [1] 32.01439
#> attr(,"scaled:scale")
#> [1] 1.798561

# and the price from Dollar to Euro
LinScale(d.pizza[1:20, "price"], 0, 1, 0, 0.76)
#>           [,1]
#>  [1,] 49.89780
#>  [2,] 20.50480
#>  [3,] 31.13720
#>  [4,] 19.74480
#>  [5,] 43.74180
#>  [6,] 10.63240
#>  [7,] 67.97592
#>  [8,]       NA
#>  [9,] 31.13720
#> [10,] 64.39860
#> [11,] 50.47160
#> [12,] 47.84580
#> [13,] 35.54064
#> [14,] 37.96200
#> [15,] 56.04696
#> [16,] 43.74180
#> [17,] 20.50480
#> [18,] 21.26480
#> [19,] 31.89720
#> [20,]  9.11240
#> attr(,"scaled:center")
#> [1] 0
#> attr(,"scaled:scale")
#> [1] 1.315789

# together
LinScale(d.pizza[1:20, c("temperature", "price")],
  0, c(100, 1), c(-17.8, 0), c(37.8, 0.76) )
#>    temperature    price
#> 1      11.6680 49.89780
#> 2      13.5584 20.50480
#> 3       2.4940 31.13720
#> 4           NA 19.74480
#> 5      10.0000 43.74180
#> 6      -2.7880 10.63240
#> 7       1.0484 67.97592
#> 8      12.6688       NA
#> 9       8.8880 31.13720
#> 10     12.4464 64.39860
#> 11     -1.7872 50.47160
#> 12     10.7228 47.84580
#> 13     -4.4282 35.54064
#> 14      2.0492 37.96200
#> 15     12.0016 56.04696
#> 16     10.7228 43.74180
#> 17     10.5560 20.50480
#> 18      8.7212 21.26480
#> 19     11.5568 31.89720
#> 20     -6.6800  9.11240
#> attr(,"scaled:center")
#> [1] 32.01439  0.00000
#> attr(,"scaled:scale")
#> [1] 1.798561 1.315789


if (FALSE) {
par(mfrow=c(3,1), mar=c(0,5,0,3), oma=c(5,0,5,0))
plot(LinScale(d.frm[,1]), ylim=c(-2,2), xaxt="n", ylab="LinScale")
plot(RobScale(d.frm[,1]), ylim=c(-2,2), xaxt="n", ylab="RobScale")
plot(scale(d.frm[,1]), ylim=c(-2,2), ylab="scale")
title("Compare scales", outer = TRUE)
}