dummy_code: Create dummy coded variables

View source: R/dummy_code.R

dummy_codeR Documentation

Create dummy coded variables

Description

Given a variable x with n distinct values, create n new dummy coded variables coded 0/1 for presence (1) or absence (0) of each variable. This function can be used to create a dummy code by splitting a single value into multiple values separated by commas or other delimiters by specifying any delimiter character.

Usage

dummy_code(x, split, variables = NULL, prefix = NULL, suffix = NULL, ...)

Arguments

x

a vector or one column of data frame to be transformed into dummy codes

split

a character (or object which can be coerced to such) containing regular expression to use for splitting. If empty matches occur, in particular if split has length 0, x is split into single characters.

variables

a vector, The default is NULL, which refers to x and estimates the specified value, but if specified it checks and applies to the variable.

prefix

a String to be prefix to the column name in the dummy code data frame

suffix

a String to be suffix to the column name in the dummy code data frame

...

Other arguments to carry over to strsplit.

Value

a data.frame of dummy coded variables.

Examples

df_sample <- data.frame(sample = c("a,b", "b", "c", "c,a", "a,b,c"))
(df_dummy <- dummy_code(df_sample$sample, split = ","))
new_df <- cbind(df_sample, df_dummy)
new_df
(df_dummy_v <- dummy_code(df_sample$sample, split = ",", variables = c("a", "b", "c", "d")))
new_df_v <- cbind(df_sample, df_dummy_v)
new_df_v

indenkun/infun documentation built on April 17, 2025, 2:52 p.m.