most_centered {rearrr}R Documentation

Find the coordinates for the data point closest to the centroid

Description

[Experimental]

Returns the coordinates for the data point with the shortest distance to the centroid.

To get a logical vector (TRUE/FALSE) indicating whether a data point is the most centered, use is_most_centered().

Usage

most_centered(..., cols = NULL, na.rm = FALSE)

Arguments

...

Numeric vectors or a single data.frame.

cols

Names of columns to use when `...` is a single data.frame.

na.rm

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

Value

The coordinates for the data point closest to the centroid. Either as a vector or a data.frame.

Author(s)

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

See Also

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

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 coordinates of the data point
# closest to the centroid
most_centered(x, y, z)

# Compare to centroid coordinates
centroid(x, y, z)

#
# For data.frames
#

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

# Find coordinates of the data point
# closest to the centroid
most_centered(df, cols = c("x", "y", "z"))

# When 'df' is grouped
df %>%
  dplyr::group_by(g) %>%
  most_centered(cols = c("x", "y", "z"))

# Filter to only include most centered data points
df %>%
  dplyr::group_by(g) %>%
  dplyr::filter(is_most_centered(x, y, z))

[Package rearrr version 0.3.4 Index]