Q:

Min-max normalization in R, setting groups of min and max based on another column

Using R, I am trying to min-max normalize a column, but instead of using the min and max of all the column values, I need to set min and max by groups that are determined by another column.
Please see this example:
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

For y=1, min(x) = 0, and max(x) = 2.5.
For y=2, min(x) = 0.2, and max(x) = 0.5, and so forth.
Based on this grouped min and max, normalization is performed. 
I found a similar question for Python, but it didnt help me much:
Normalize a column of dataframe using min max normalization based on groupby of another column

A:

library(tidyverse)

df %>%
  group_by(y) %>%
  mutate(xnorm = (x - min(x)) / (max(x) - min(x))) %>%
  ungroup()

Output:
# 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 

Or, in the mutate() statement, you could put xnorm = scales::rescale(x)

