# \donttest{
# Attach packages
library(rearrr)
library(dplyr)
# Set seed
set.seed(1)
# Create a data frame
df <- data.frame(
"index" = 1:10,
"A" = sample(1:10),
"B" = runif(10),
"G" = c(
1, 1, 1, 2, 2,
2, 3, 3, 3, 3
),
stringsAsFactors = FALSE
)
# Furthest from 3 in a vector
furthest_from_vec(1:10, origin = 3)
# Furthest from the third row (index of data.frame)
furthest_from(df, origin = 3)$index
# By each of the columns
furthest_from(df, cols = "A", origin = 3)$A
furthest_from(df, cols = "A", origin_fn = most_centered)$A
furthest_from(df, cols = "B", origin = 0.5)$B
furthest_from(df, cols = "B", origin_fn = centroid)$B
# Shuffle the elements with the same distance to the origin
furthest_from(df,
cols = "A",
origin_fn = create_origin_fn(median),
shuffle_ties = TRUE
)$A
# Grouped by G
df %>%
dplyr::select(G, A) %>% # For clarity
dplyr::group_by(G) %>%
furthest_from(
cols = "A",
origin_fn = create_origin_fn(median)
)
# Plot the rearranged values
plot(
x = 1:10,
y = furthest_from(df,
cols = "B",
origin_fn = create_origin_fn(median)
)$B,
xlab = "Position", ylab = "B"
)
plot(
x = 1:10,
y = furthest_from(df,
cols = "A",
origin_fn = create_origin_fn(median),
shuffle_ties = TRUE
)$A,
xlab = "Position", ylab = "A"
)
# In multiple dimensions
df %>%
furthest_from(cols = c("A", "B"), origin_fn = most_centered)
# }
Run the code above in your browser using DataLab