简体   繁体   English

R 用 1 到 -1 之间的负数和正数缩放向量

[英]R Scale a vector with negative and positive numbers between 1 and -1

I have a vector as follows我有一个向量如下

vec <- c(0, -0.072, -0.092, -0.092, -0.222, -0.445, -0.345, -0.031, 
0.016, 0.158, 0.349, 0.749, 1.182, 1.289, 1.578, 1.767, 1.621, 
1.666, 1.892, 1.866, 1.821, 1.702, 1.69, 1.53, 1.38, 1.494, 1.833, 
2.392, 2.502, 2.921, 3.363, 3.698, 3.645, 3.89, 3.987, 4.066, 
3.963, 3.749, 3.512, 3.259, 3.153, 2.972, 2.918, 2.93, 2.719, 
2.458, 2.275, 2.346, 2.588, 2.774, 2.607, 2.336, 1.799, 1.365, 
1.025, 0.379, -0.087, -0.765, -1.19, -1.423, -1.751, -1.965, 
-1.907, -1.919, -1.848, -1.772, -1.49, -1.19, -1.104, -1.138, 
-1.054, -1.139, -1.269, -1.429, -1.56, -1.543, -1.364, -1.318, 
-1.094, -1.061, -0.918, -0.861, -0.913, -0.767, -0.615, -0.532, 
-0.615, -0.688, -0.75, -0.724, -0.755, -0.685, -0.752, -0.863, 
-0.944, -1.004, -1.02, -1.041, -1.073, -1.392)

The following code scales this vector between 1 and -1 perfectly fine.下面的代码在 1 和 -1 之间完美地缩放这个向量。

scale <- function(input)
{
  min_val = min(input, na.rm = T)
  max_val = max(input, na.rm = T)
  
  average = (min_val + max_val) / 2
  range = (max_val - min_val) / 2
  normalized_x = (input - average) / range
  return(normalized_x)
}

However, I want to scale this vector from -1 to 1 while keeping the midpoint at 0.但是,我想将此向量从 -1 缩放到 1,同时保持中点为 0。

Can someone please improve the above function to center this scaling around 0?有人可以改进上面的 function 以使这个缩放围绕 0 居中吗?

Thanks!谢谢!

Calling this operation "normalization" is rather confusing.将此操作称为“规范化”是相当令人困惑的。 The correct term is scaling.正确的术语是缩放。 Normalization means you have transformed the values to something resembling a Normal distribution.归一化意味着您已将值转换为类似于正态分布的东西。 (There is an R function named scale ,) (有一个名为scale的 R function ,)

This will scale the values below 0 to the range [-1, 0) and the values above 0 to the range (0,1] which is what I understand to be the desire:这会将低于 0 的值缩放到范围 [-1, 0) 并将高于 0 的值缩放到范围 (0,1] 这是我理解的愿望:

c( -vec[vec<0]/min(vec), vec[vec>=0]/max(vec) )

They are not in the original order, however.但是,它们不是原始顺序。 If that is desired, you might need to put an ifelse operation in place:如果需要,您可能需要执行ifelse操作:

 newvec <- ifelse(vec < 0 , -vec[vec<0]/min(vec), vec[vec>=0]/max(vec)  )
  #-------------
 > sum(newvec<0)
[1] 51
> sum(newvec>0)
[1] 47
> sum(newvec==0)
[1] 2

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM