官方例子如下,
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