# Attach package
library(rearrr)
library(dplyr)
# Create a data frame
# We group it by G so we have 3 groups
df <- data.frame(
"Index" = 1:12,
"A" = c(1:4, 9:12, 15:18),
"G" = rep(1:3, each = 4)
) %>%
dplyr::group_by(G)
# Create new pipeline
pipe <- FixedGroupsPipeline$new(num_groups = 3)
# Add 2D rotation transformation
pipe$add_transformation(
fn = rotate_2d,
args = list(
x_col = "Index",
y_col = "A",
suffix = "",
overwrite = TRUE
),
var_args = list(
degrees = list(45, 90, 180),
origin = list(c(0, 0), c(1, 2), c(-1, 0))
),
name = "Rotate"
)
# Add the `cluster_group` transformation
# As the function is fed an ungrouped subset of `data`,
# i.e. the rows of that group, we need to specify `group_cols` in `args`
# That is specific to `cluster_groups()` though
# Also note `.apply` in `var_args` which tells the pipeline *not*
# to apply this transformation to the second group
pipe$add_transformation(
fn = cluster_groups,
args = list(
cols = c("Index", "A"),
suffix = "",
overwrite = TRUE,
group_cols = "G"
),
var_args = list(
multiplier = list(0.5, 1, 5),
.apply = list(TRUE, FALSE, TRUE)
),
name = "Cluster"
)
# Check pipeline object
pipe
# Apply pipeline to already grouped data.frame
# Enable `verbose` to print progress
pipe$apply(df, verbose = TRUE)
Run the code above in your browser using DataLab