Масштабирование столбцов матрицы в программировании на R - функция scale ()
scale()
function in R Langauge is a generic function which centers and scales the columns of a numeric matrix. The center
parameter takes either numeric alike vector or logical value. If the numeric vector is provided, then each column of the matrix has the corresponding value from center
subtracted from it. If the logical value is provided TRUE, then column means of the matrix is subtracted from their corresponding columns. The scale
takes either numeric alike vector or logical value. When provided with a numeric like vector, then each column of the matrix is divided by the corresponding value from scale
. If the logical value is provided in scale
parameter, then centered columns of the matrix is divided by their standard deviations, and the root mean square otherwise. If FALSE, no scaling is done on the matrix.
Syntax:
scale(x, center = TRUE, scale = TRUE)Parameters:
x: represents numeric matrix
center: represents either logical value or numeric alike vector equal to the number of x
scale: represents either logical value or numeric alike vector equal to the number of x
Example 1:
# Create matrix mt <- matrix (1:10, ncol = 5) # Print matrix cat ( "Matrix:
" ) print (mt) # Scale matrix with default arguments cat ( "
After scaling:
" ) scale (mt) |
Выход:
Матрица: [, 1] [, 2] [, 3] [, 4] [, 5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10 После масштабирования: [, 1] [, 2] [, 3] [, 4] [, 5] [1,] -0,7071068 -0,7071068 -0,7071068 -0,7071068 -0,7071068 [2,] 0,7071068 0,7071068 0,7071068 0,7071068 0,7071068 attr (, "масштабировано: центр") [1] 1,5 3,5 5,5 7,5 9,5 attr (, "масштабировано: масштаб") [1] 0,7071068 0,7071068 0,7071068 0,7071068 0,7071068
Example 2:
# Create matrix mt <- matrix (1:10, ncol = 2) # Print matrix cat ( "Matrix:
" ) print (mt) # Scale center by vector of values cat ( "
Scale center by vector of values:
" ) scale (mt, center = c (1, 2), scale = FALSE ) # Scale by vector of values cat ( "
Scale by vector of values:
" ) scale (mt, center = FALSE , scale = c (1, 2)) |
Выход:
Матрица: [, 1] [, 2] [1,] 1 6 [2,] 2 7 [3,] 3 8 [4,] 4 9 [5,] 5 10 Центр шкалы по вектору значений: [, 1] [, 2] [1,] 0 4 [2,] 1 5 [3,] 2 6 [4,] 3 7 [5,] 4 8 attr (, "масштабировано: центр") [1] 1 2 Масштабирование по вектору значений: [, 1] [, 2] [1,] 1 3,0 [2,] 2 3,5 [3,] 3 4,0 [4,] 4 4,5 [5,] 5 5,0 attr (, "масштабировано: масштаб") [1] 1 2