assign_factorial_levels {basecamb}R Documentation

Assign custom values for key levels in factorial columns

Description

Use a named vector of keys (current value) and values for factorial columns to assign meaningful levels and/or group levels

Usage

assign_factorial_levels(
  data,
  factor_keys_values,
  na_action_default = "keep_NA"
)

Arguments

data

data.frame to modify

factor_keys_values

named list with:

  • Keys: Names of factor columns

  • values: Named vectors with

    • keys: current value (string representation)

    • values: new value to be assigned

    • if a 'default' key is passed, all existing values not conforming to the new scheme will be converted to the 'default' value

    • if a 'NA' key is passed, all NA values will be converted to the value specified here. Overwrites na_action_default for the specified column.

na_action_default

character: Specify what to do with NA values. Defaults to 'keep_NA'. Options are:

  • 'keep_NA' NA values remain NA values

  • 'assign_default' NA values are assigned the value specified as 'default'. Requires a 'default' value to be specified Can be overwritten for individal columns by specifying a value for key 'NA'

Value

data frame with new levels

Author(s)

J. Peter Marquardt

Examples

data <- data.frame(col1 = as.factor(rep(c('1', '2', '4'), 5)))
keys_1 <- list('col1' = c('1' = 'One', '2' = 'Two', '4' = 'Four'))
data_1 <- assign_factorial_levels(data, keys_1)
keys_2 <- list('col1' = c('1' = 'One', 'default' = 'Not_One'))
data_2 <- assign_factorial_levels(data, keys_2)


[Package basecamb version 1.1.5 Index]