Normalize a vector in R
Rescales a vector to zero mean and unit variance, the transform every model wants.
normalize <- function(x) {
rng <- range(x, na.rm = TRUE)
if (rng[1] == rng[2]) {
return(rep(0, length(x)))
}
(x - rng[1]) / (rng[2] - rng[1])
}
Keywords and builtins used here
functioniflengthrangerepreturn
The run, in numbers
- Lines
- 7
- Characters to type
- 143
- Tokens
- 64
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 43 seconds.
Step 3 of 5 in Vectorized thinking, step 12 of 20 in Language basics.