R/ordinal_to_binary.R
ordinal_to_binary.Rd
convert a column of categorical covariates into a number of columns with a binary flag for each category
ordinal_to_binary_(df, col_name, prefix = NULL, overwrite = FALSE)
df | data frame |
---|---|
col_name | column name to split into multiple binary flags |
prefix | string name if want to specify a prefix to label the binary flag columns other than the original col_name |
overwrite | overwrite any existing columns if the newly generated columns share the same name |
#> OCC OCC1 OCC2 OCC3 #> 1 1 1 0 0 #> 2 1 1 0 0 #> 3 2 0 1 0 #> 4 3 0 0 1df %>% ordinal_to_binary_("OCC", prefix = "OCCASION")#> OCC OCCASION1 OCCASION2 OCCASION3 #> 1 1 1 0 0 #> 2 1 1 0 0 #> 3 2 0 1 0 #> 4 3 0 0 1#> Warning: detected existing columns with same name sequence, #> prepending "_" to all generated columns.#> OCC OCC1 _OCC1 _OCC2 _OCC3 #> 1 1 999 1 0 0 #> 2 1 999 1 0 0 #> 3 2 999 0 1 0 #> 4 3 999 0 0 1df2 %>% ordinal_to_binary_("OCC", overwrite=T)#> OCC OCC1 OCC2 OCC3 #> 1 1 1 0 0 #> 2 1 1 0 0 #> 3 2 0 1 0 #> 4 3 0 0 1