View source: R/design_structure.R
| encode_factors | R Documentation |
Turns categorical columns into the numeric columns a model needs. Dummy
coding is one indicator per level, with no reference level dropped; effect
coding drops the last level's indicator and codes that level -1 across the
remaining columns; factor coding leaves one factor column.
encode_factors(
data,
variables,
coding = c("factor", "dummy", "effect"),
labels = NULL,
prefix = "f_",
replace = FALSE
)
data |
Base |
variables |
Categorical variables to encode. A character vector of at
least one column name of |
coding |
Encoding to apply, one of |
labels |
Optional replacement level labels, as a named list per
variable, one atomic vector shared by every variable, or a tidy data frame
with columns |
prefix |
Prefix for generated variables. A single string, defaulting to
|
replace |
Remove the source variables after encoding. A single flag,
defaulting to |
A simulab_sim base data.frame with the columns of data, minus
the source variables when replace is TRUE, followed by the generated
columns, one row per input row. The component encoding, reached with
as.data.frame(x, what = "encoding"), holds one row per generated column
with columns source, encoded and coding.
data <- data.frame(id = 1:6, arm = rep(c("a", "b", "c"), each = 2))
encode_factors(data, variables = "arm", coding = "dummy")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.