# Attach package
library(rearrr)
# Create a data frame
df <- data.frame(
"Index" = 1:12,
"A" = c(1:4, 9:12, 15:18),
"G" = rep(1:3, each = 4)
)
# Create new pipeline
pipe <- GeneratedPipeline$new()
# Add 2D rotation transformation
# Note that we specify the grouping via `group_cols`
pipe$add_transformation(
fn = rotate_2d,
args = list(
x_col = "Index",
y_col = "A",
suffix = "",
overwrite = TRUE
),
generators = list(degrees = function(){sample.int(360, 1)},
origin = function(){rnorm(2)}),
name = "Rotate",
group_cols = "G"
)
# Add the `cluster_group` transformation
# Note that this function requires the entire input data
# to properly scale the groups. We therefore specify `group_cols`
# as part of `args`. This works as `cluster_groups()` accepts that
# argument.
# Also note the `.apply` generator which generates a TRUE/FALSE scalar
# for whether the transformation should be applied to the current group
pipe$add_transformation(
fn = cluster_groups,
args = list(
cols = c("Index", "A"),
suffix = "",
overwrite = TRUE,
group_cols = "G"
),
generators = list(
multiplier = function() {
0.1 * runif(1) * 3 ^ sample.int(5, 1)
},
.apply = function(){sample(c(TRUE, FALSE), 1)}
),
name = "Cluster"
)
# Check pipeline object
pipe
# Apply pipeline to data.frame
# Enable `verbose` to print progress
pipe$apply(df, verbose = TRUE)
## ------------------------------------------------
## Method `GeneratedPipeline$add_transformation`
## ------------------------------------------------
# `generators` is a list of functions for generating
# argument values for a chosen set of arguments
# `.apply` can be used to disable the transformation
generators = list(degrees = function(){sample.int(360, 1)},
origin = function(){rnorm(2)},
.apply = function(){sample(c(TRUE, FALSE), 1)})
Run the code above in your browser using DataLab