3

Imagine I have a vector of values in R. It's super long but just do give you a sample:

[1] 2782.779 2781.655 2780.531 2779.407 2778.282 2777.898 2777.514 
2777.130 2776.746 2776.471 2776.196 2775.921
[13] 2775.646 2775.055 2774.463 2773.872 2773.281 2774.238 2775.195 
2776.152 2777.109 2776.905 2776.701 2776.497
[25] 2776.293 2775.658 2775.024 2774.389 2773.755 2774.214 2774.673 
2775.132 2775.591 2777.585 2779.579 2781.574
[37] 2783.568 2787.358 2791.149 2794.940 2798.731 2802.518 2806.305 
2810.092 2813.879 2814.760 2815.641 2816.522

I want to scale/normalize each value in the vector so it ranges between 0 and 1. The max value of the dataset would take on a value of 1 and the min value would take on a value of 0. Are there any existing functions/libraries to easily implement this?

Thanks!

Jane Sully
  • 3,137
  • 10
  • 48
  • 87

1 Answers1

13

Note that here, normalized values = (value-min)/(max-min). So try this:

v1 <- 1:100
v2 <- (v1-min(v1))/(max(v1)-min(v1))
v2

or just v2 <- scales::rescale(v1, to=c(0,1))

nghauran
  • 6,648
  • 2
  • 20
  • 29