# 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)
)
# Calculate distances in the two dimensions (x and y)
# With the origin at x=0.5, y=0.5
distance(
data = df,
cols = c("x", "y"),
origin = c(0.5, 0.5)
)
# Calculate distances to the centroid for each group in 'g'
distance(
data = dplyr::group_by(df, g),
cols = c("x", "y"),
origin_fn = centroid
)
Run the code above in your browser using DataLab