# Attach packages
library(rearrr)
library(dplyr)
# Set seed
set.seed(1)
# Create a data frame
df <- data.frame(
"x" = runif(20),
"y" = runif(20),
"g" = rep(1:4, each = 5)
)
# Scale row-wise
to_unit_length(df, cols = c("x", "y"), by_row = TRUE)
# Scale column-wise
to_unit_length(df, cols = c("x", "y"), by_row = FALSE)
# Overwrite columns
to_unit_length(df, cols = c("x", "y"), suffix = "", overwrite = TRUE)
# By groups in 'g'
df %>%
dplyr::group_by(g) %>%
to_unit_length(cols = c("x", "y"), by_row = FALSE)
# Scale a vector
to_unit_length_vec(c(1:10))
to_unit_length(c(1:10), suffix = "", overwrite = TRUE)
vector_length(to_unit_length_vec(c(1:10)))
Run the code above in your browser using DataLab