R语言将所有列数据正交化/缩放

官方例子如下,

rescale01 <- function(x) {
  rng <- range(x, na.rm = TRUE)
  (x - rng[1]) / (rng[2] - rng[1])
}
df <- tibble(x = 1:4, y = rnorm(4))
df %>% mutate(across(where(is.numeric), rescale01))
#> # A tibble: 4 x 2
#>       x     y
#>   <dbl> <dbl>
#> 1 0     0.385
#> 2 0.333 1    
#> 3 0.667 0    
#> 4 1     0.903

https://dplyr.tidyverse.org/articles/colwise.html

posted @ 2020-11-21 17:11  2021年的顺遂平安君  阅读(461)  评论(0编辑  收藏  举报