transfer_centroids {rearrr}R Documentation

Transfer centroids from one data frame to another

Description

[Experimental]

Given two data.frames with the same columns (and groupings), transfer the centroids from one to the other.

This is commonly used to restore the centroids after transforming the columns.

Usage

transfer_centroids(to_data, from_data, cols, group_cols = NULL)

Arguments

to_data

data.frame.

Existing `dplyr` groups are ignored. Specify in `group_cols` instead.

from_data

data.frame with the same columns (and groupings) as `to_data`.

Existing `dplyr` groups are ignored. Specify in `group_cols` instead.

cols

Names of numeric columns to transfer centroids to. Must exist in both `to_data` and `from_data`.

group_cols

Names of grouping columns.

Value

The `to_data` data.frame (tibble) with the centroids from the `from_data` data.frame.

Author(s)

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

See Also

Other clustering functions: cluster_groups(), generate_clusters()

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

# Create another data frame with different x and y values
df2 <- df
df2$x <- runif(20)
df2$y <- runif(20)

# Check centroids before transfer

df %>%
  dplyr::group_by(g) %>%
  dplyr::summarize_all(mean)

df2 %>%
  dplyr::group_by(g) %>%
  dplyr::summarize_all(mean)

# Now let's transfer the centroids from df to df2

df3 <- transfer_centroids(
  to_data = df2,
  from_data = df,
  cols = c("x", "y"),
  group_cols = "g"
)

# Check that the transfer gave us the same centroids as df
df3 %>%
  dplyr::group_by(g) %>%
  dplyr::summarize_all(mean)

[Package rearrr version 0.3.4 Index]