18

这是我拥有的数字矩阵的摘录

 [1,]   30 -33.129487   3894754.1 -39.701738 -38.356477 -34.220534
 [2,]   29 -44.289487  -8217525.9 -44.801738 -47.946477 -41.020534
 [3,]   28 -48.439487  -4572815.9 -49.181738 -48.086477 -46.110534
 [4,]   27 -48.359487  -2454575.9 -42.031738 -43.706477 -43.900534
 [5,]   26 -38.919487  -2157535.9 -47.881738 -43.576477 -46.330534
 [6,]   25 -45.069487  -5122485.9 -47.831738 -47.156477 -42.860534
 [7,]   24 -46.207487  -2336325.9 -53.131738 -50.576477 -50.410534
 [8,]   23 -51.127487  -2637685.9 -43.121738 -47.336477 -47.040534
 [9,]   22 -45.645487   3700424.1 -56.151738 -47.396477 -50.720534
[10,]   21 -56.739487   1572594.1 -49.831738 -54.386577 -52.470534
[11,]   20 -46.319487    642214.1 -39.631738 -44.406577 -41.490534

我现在要做的是将每列的值缩放为从 0 到 1 的值。

我尝试使用scale()矩阵上的函数(默认参数)来完成此操作,我得到了这个

[1,] -0.88123100  0.53812440 -1.05963281 -1.031191482 -0.92872324
 [2,] -1.17808251 -1.13538649 -1.19575096 -1.289013031 -1.11327085
 [3,] -1.28847084 -0.63180980 -1.31265244 -1.292776849 -1.25141017
 [4,] -1.28634287 -0.33914007 -1.12182012 -1.175023107 -1.19143220
 [5,] -1.03524267 -0.29809911 -1.27795565 -1.171528133 -1.25738083
 [6,] -1.19883019 -0.70775576 -1.27662116 -1.267774342 -1.16320727
 [7,] -1.22910054 -0.32280189 -1.41807728 -1.359719044 -1.36810940
 [8,] -1.35997055 -0.36443973 -1.15091204 -1.272613537 -1.27664977
 [9,] -1.21415156  0.51127451 -1.49868058 -1.274226602 -1.37652260
[10,] -1.50924749  0.21727976 -1.33000083 -1.462151358 -1.42401647
[11,] -1.23207969  0.08873245 -1.05776452 -1.193844887 -1.12602635

这已经接近我想要的,但 0:1 的值甚至更好。我阅读了 的帮助手册scale(),但我真的不明白我该怎么做。

4

5 回答 5

26

尝试以下,这似乎很简单:

## Data to make a minimal reproducible example
m <- matrix(rnorm(9), ncol=3)

## Rescale each column to range between 0 and 1
apply(m, MARGIN = 2, FUN = function(X) (X - min(X))/diff(range(X)))
#           [,1]      [,2]      [,3]
# [1,] 0.0000000 0.0000000 0.5220198
# [2,] 0.6239273 1.0000000 0.0000000
# [3,] 1.0000000 0.9253893 1.0000000
于 2013-03-18T02:29:59.730 回答
25

如果您仍然要使用scale

maxs <- apply(a, 2, max)
mins <- apply(a, 2, min)
scale(a, center = mins, scale = maxs - mins)
于 2013-03-18T02:41:09.783 回答
3

安装clusterSim包并运行以下命令:

normX = data.Normalization(x,type="n4");
于 2013-06-13T20:03:18.337 回答
0

不是最漂亮的,但这只是完成了工作,因为我需要在数据框中执行此操作。

column_zero_one_range_scale  <- function(
input_df,
columns_to_scale #columns in input_df to scale, must be numeric
){

input_df_replace <- input_df

columncount <- length(columns_to_scale)
for(i in 1:columncount){

columnnum <- columns_to_scale[i]

if(class(input_df[,columnnum]) !='numeric' & class(input_df[,columnnum])!='integer')
  {print(paste('Column name ',colnames(input_df)[columnnum],' not an integer or numeric, will skip',sep='')) }


if(class(input_df[,columnnum]) %in% c('numeric','integer'))
{
  vec <- input_df[,columnnum]
  rangevec <- max(vec,na.rm=T)-min(vec,na.rm=T)
  vec1 <- vec - min(vec,na.rm=T)
  vec2 <- vec1/rangevec
}
input_df_replace[,columnnum] <- vec2
colnames(input_df_replace)[columnnum] <- paste(colnames(input_df)[columnnum],'_scaled')

}

return(input_df_replace)


}
于 2018-02-08T15:38:46.773 回答
0

scales包有一个名为rescale

set.seed(2020)

x <- runif(5, 100, 150)
scales::rescale(x)
#1.0000000 0.5053362 0.9443995 0.6671695 0.0000000
于 2020-11-22T18:49:06.037 回答