flip_values {rearrr}R Documentation

Flip the values around an origin

Description

[Experimental]

The values are flipped with the formula `x = 2 * c - x` where x is the value and c is the origin coordinate to flip the values around.

The origin can be supplied as coordinates or as a function that returns coordinates. The latter can be useful when supplying a grouped data.frame and flipping around e.g. the centroid of each group. By default the median value in each dimension is used.

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

Example:

The column values:

c(5, 2, 7, 4, 3, 1)

and the origin_fn = create_origin_fn(median)

Changes the values to :

c(2, 5, 0, 3, 4, 6)

Usage

flip_values(
  data,
  cols = NULL,
  origin = NULL,
  origin_fn = create_origin_fn(median),
  suffix = "_flipped",
  keep_original = TRUE,
  origin_col_name = ".origin",
  overwrite = FALSE
)

flip_values_vec(data, origin = NULL, origin_fn = create_origin_fn(median))

Arguments

data

data.frame or vector.

cols

Names of columns in `data` to flip values of.

origin

Coordinates of the origin to flip around. A scalar to use in all dimensions (columns) or a vector with one scalar per dimension.

N.B. Ignored when `origin_fn` is not NULL. Remember to set it to NULL when passing origin coordinates manually!

origin_fn

Function for finding the origin coordinates.

Input: Each column will be passed as a vector in the order of `cols`.

Output: A vector with one scalar per dimension.

Can be created with create_origin_fn() if you want to apply the same function to each dimension.

E.g. `create_origin_fn(median)` would find the median of each column.

Built-in functions are centroid(), most_centered(), and midrange()

suffix

Suffix to add to the names of the generated columns.

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

keep_original

Whether to keep the original columns. (Logical)

Some columns may have been overwritten, in which case only the newest versions are returned.

origin_col_name

Name of new column with the origin coordinates. If NULL, no column is added.

overwrite

Whether to allow overwriting of existing columns. (Logical)

Author(s)

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

See Also

Other mutate functions: apply_transformation_matrix(), cluster_groups(), dim_values(), expand_distances(), expand_distances_each(), roll_values(), rotate_2d(), rotate_3d(), shear_2d(), shear_3d(), swirl_2d(), swirl_3d()

Examples

# Attach packages
library(rearrr)
library(dplyr)
has_ggplot <- require(ggplot2)  # Attach if installed

# Set seed
set.seed(1)

# Create a data frame
df <- data.frame(
  "Index" = 1:10,
  "A" = sample(1:10),
  "B" = runif(10),
  "G" = c(
    1, 1, 1, 2, 2,
    2, 3, 3, 3, 3
  ),
  stringsAsFactors = FALSE
)

# Flip values of the columns
flip_values(df$A)
flip_values(df, cols = "A")
flip_values(df, cols = "B", origin = 0.3, origin_fn = NULL, keep_original = FALSE)
flip_values(df,
  cols = c("A", "B"),
  origin = c(3, 0.3),
  origin_fn = NULL,
  suffix = "",
  keep_original = FALSE,
  overwrite = TRUE
)
flip_values(df, cols = c("A", "B"), origin_fn = create_origin_fn(max))

# Grouped by G
df %>%
  dplyr::group_by(G) %>%
  flip_values(
    cols = c("A", "B"),
    origin_fn = create_origin_fn(median),
    keep_original = FALSE
  )

# Plot A and flipped A

# First flip A around the median and then around the value 3.
df <- df %>%
  flip_values(cols = "A", suffix = "_flip_median", origin_col_name = NULL) %>%
  flip_values(cols = "A", suffix = "_flip_3", origin = 3,
              origin_fn = NULL, origin_col_name = NULL)

# Plot A and A flipped around its median
if (has_ggplot){
  ggplot(df, aes(x = Index, y = A)) +
    geom_line(aes(color = "A")) +
    geom_line(aes(y = A_flip_median, color = "Flipped A (median)")) +
    geom_hline(aes(color = "Median A", yintercept = median(A))) +
    theme_minimal()
}

# Plot A and A flipped around the value 3
if (has_ggplot){
  ggplot(df, aes(x = Index, y = A)) +
    geom_line(aes(color = "A")) +
    geom_line(aes(y = A_flip_3, color = "Flipped A (3)")) +
    geom_hline(aes(color = "3", yintercept = 3)) +
    theme_minimal()
}

[Package rearrr version 0.3.4 Index]