is_most_centered {rearrr}R Documentation

Find which data point is closest to the centroid

Description

[Experimental]

Finds the data point with the shortest distance to the centroid.

To get the coordinates of the most centered data point, use most_centered() instead.

Usage

is_most_centered(..., na.rm = FALSE)

Arguments

...

Numeric vectors.

na.rm

Whether to ignore missing values. At least one data point must be complete. (Logical)

Value

Logical vector (TRUE/FALSE) indicating if a data point is the most centered.

Author(s)

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

See Also

Other coordinate functions: centroid(), create_origin_fn(), midrange(), most_centered()

Examples


# Attach packages
library(rearrr)
library(dplyr)

# Set seed
set.seed(1)

# Create three vectors
x <- runif(10)
y <- runif(10)
z <- runif(10)

# Find the data point closest to the centroid
is_most_centered(x, y, z)

# Compare to coordinates for the most centered
most_centered(x, y, z)

#
# For data.frames
#

# Create data frame
df <- data.frame(
  "x" = x,
  "y" = y,
  "z" = z,
  "g" = rep(1:2, each = 5)
)

# Filter the data points
# closest to the centroid
df %>%
  dplyr::filter(is_most_centered(x, y, z))

# When 'df' is grouped
df %>%
  dplyr::group_by(g) %>%
  dplyr::filter(is_most_centered(x, y, z))

# Add as column
df %>%
  dplyr::group_by(g) %>%
  dplyr::mutate(mc = is_most_centered(x, y, z))


[Package rearrr version 0.3.4 Index]