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