standardize_cols: Standardize columns of supplied matrix

Description Usage Arguments Value Examples

Description

Standardizes the columns of a supplied matrix from -1 to 1 based on a supplied data frame of ranges. Also reverses standardization based on the same inputs.

Usage

1
standardize_cols(StartingMat, column_names, Input_range, reverse_standard = FALSE)

Arguments

StartingMat

Data frame or matrix with named columns to be standardized.

column_names

Vector of column names to be standardized by this operation. Columns that are not included in this vector will not be modified.

Input_range

Range of basic input variables in the input data frame. Column names must match StartingMat column names. Format is a matrix or data frame with column in Input_range matching each column name in StartingMat and column_names, minimum value in first row, and maximum value in second row.

reverse_standard

TRUE/FALSE binary value indicating Whether standardize (FALSE) or reverse a standardization (TRUE) of the supplied matrix.

Value

Returns a matrix with the same column names as the supplied matrix where the columns in column_names have been standardized or reversed.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (StartingMat, column_names, Input_range, reverse_standard = FALSE)
{
    StandardMat <- StartingMat
    for (i in 1:length(column_names)) {
        if (reverse_standard == FALSE) {
            if (missing(Input_range)) {
                currentmax <- max(StartingMat[, column_names[i]])
                currentmin <- min(StartingMat[, column_names[i]])
                currentavg <- (currentmax + currentmin)/2
                currentrange <- currentmax - currentmin
            }
            else {
                currentmax <- max(Input_range[, column_names[i]])
                currentmin <- min(Input_range[, column_names[i]])
                currentavg <- (currentmax + currentmin)/2
                currentrange <- currentmax - currentmin
            }
            if (currentrange == 0) {
                StandardMat[, column_names[i]] <- 0
            }
            else {
                StandardMat[, column_names[i]] <- 2 * (StartingMat[,
                  column_names[i]] - currentavg)/currentrange
            }
        }
        else {
            if (missing(Input_range)) {
            }
            else {
                currentmax <- max(Input_range[, column_names[i]])
                currentmin <- min(Input_range[, column_names[i]])
                currentavg <- (currentmax + currentmin)/2
                currentrange <- currentmax - currentmin
                StandardMat[, column_names[i]] <- (currentrange/2) *
                  (StartingMat[, column_names[i]]) + currentavg
            }
        }
    }
    return(StandardMat)
  }

taalbrecht/MultiEqOptimizer documentation built on May 31, 2019, 12:51 a.m.