R 中的最小-最大归一化,根据另一列设置最小和最大组

Jau*_*ume 6 r normalization

使用 R,我尝试对列进行最小-最大标准化,但我需要按由另一列确定的组来设置最小值和最大值,而不是使用所有列值的最小值和最大值。

请看这个例子:

x <- c(0, 0.5, 1, 2.5, 0.2, 0.3, 0.5, 0,0,0.1, 0.7)
y <- c(1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3)

df <- data.frame (x, y)

df
Run Code Online (Sandbox Code Playgroud)

对于 y=1,min(x) = 0,max(x) = 2.5。对于 y=2,min(x) = 0.2,max(x) = 0.5,依此类推。

根据该分组的最小值和最大值,执行归一化。

我发现了一个类似的Python问题,但它对我没有多大帮助: Normalize a column of dataframe using min max normalization based on groupby of another column

Nik*_*Nik 5

library(tidyverse)

df %>%
  group_by(y) %>%
  mutate(xnorm = (x - min(x)) / (max(x) - min(x))) %>%
  ungroup()
Run Code Online (Sandbox Code Playgroud)

输出:

# A tibble: 11 x 3
       x     y xnorm
   <dbl> <dbl> <dbl>
 1   0       1 0    
 2   0.5     1 0.2  
 3   1       1 0.4  
 4   2.5     1 1    
 5   0.2     2 0    
 6   0.3     2 0.333
 7   0.5     2 1    
 8   0       3 0    
 9   0       3 0    
10   0.1     3 0.143
11   0.7     3 1 
Run Code Online (Sandbox Code Playgroud)

或者,在mutate()声明中,您可以输入xnorm = scales::rescale(x)