# 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)
Run the code above in your browser using DataLab