Expand categorical attribute variables to a series of dichotomous variables

expand_attributes(
  data,
  attributes,
  prefix = "",
  glue = "__",
  suffix = "",
  falseValue = 0,
  trueValue = 1,
  valueFirst = TRUE,
  append = TRUE
)

Arguments

data

The data frame, normally the $mergedSources data frame that exists in the object returned by a call to parse_sources().

attributes

The name of the attribute(s) to expand.

prefix, suffix

The prefix and suffix to add to the variables names that are returned.

glue

The glue to paste the first part ad the second part of the composite variable name together.

falseValue, trueValue

The values to set for rows that, respectively, do not match and do match an attribute value.

valueFirst

Whether to insert the attribute value first, or the attribute name, in the composite variable names.

append

Whether to append the columns to the supplied data frame or not.

Value

A data.frame

Examples

### Get path to example source examplePath <- system.file("extdata", package="rock"); ### Get a path to one example file exampleFile <- file.path(examplePath, "example-1.rock"); ### Parse single example source parsedExample <- rock::parse_source(exampleFile); ### Create a categorical attribute column parsedExample$mergedSourceDf$age_group <- rep(c("18", "18-30", "31-60", ">60"), each=13); ### Expand to four logical columns parsedExample$mergedSourceDf <- rock::expand_attributes( parsedExample$mergedSourceDf, "age_group", valueFirst = FALSE ); ### Show result table(parsedExample$mergedSourceDf$age_group, parsedExample$mergedSourceDf$age_group__.18);
#> Error in table(parsedExample$mergedSourceDf$age_group, parsedExample$mergedSourceDf$age_group__.18): all arguments must have the same length
table(parsedExample$mergedSourceDf$age_group, parsedExample$mergedSourceDf$age_group__18.30);
#> #> 0 1 #> >60 13 0 #> 18 13 0 #> 18-30 0 13 #> 31-60 13 0