# 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),
"C" = LETTERS[1:10],
"G" = c(
1, 1, 1, 2, 2,
2, 3, 3, 3, 3
),
stringsAsFactors = FALSE
)
# Center by the index (row numbers)
center_min(df)
# Center by each of the columns
center_min(df, col = "A")
center_min(df, col = "B")
center_min(df, col = "C")
# Randomize which elements are left and right of the center
center_min(df, col = "A", shuffle_sides = TRUE)
# Grouped by G
df %>%
dplyr::select(G, A) %>% # For clarity
dplyr::group_by(G) %>%
center_min(col = "A")
# Plot the centered values
plot(x = 1:10, y = center_min(df, col = "B")$B)
plot(x = 1:10, y = center_min(df, col = "B", shuffle_sides = TRUE)$B)
Run the code above in your browser using DataLab