to_unit_length {rearrr}R Documentation

Scale to unit length

Description

[Experimental]

Scales the vectors to unit length row-wise or column-wise.

The *_vec() version take and return a vector.

Usage

to_unit_length(
  data,
  cols = NULL,
  by_row = is.data.frame(data),
  suffix = ifelse(isTRUE(by_row), "_row_unit", "_col_unit"),
  overwrite = FALSE
)

to_unit_length_vec(data)

Arguments

data

data.frame or vector.

cols

Names of columns in `data` to scale.

by_row

Whether to scale row vectors instead of column vectors. (Logical)

Note: Disable when `data` is a vector.

suffix

Suffix to add to the names of the generated columns.

Use an empty string (i.e. "") to overwrite the original columns.

overwrite

Whether to allow overwriting of existing columns. (Logical)

Value

Scaled vector or data.frame (tibble) with the scaled columns.

Author(s)

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

See Also

Other scaling functions: min_max_scale()

Examples

# 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)))

[Package rearrr version 0.3.4 Index]