dummy_code | R Documentation |
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.
dummy_code(x, split, variables = NULL, prefix = NULL, suffix = NULL, ...)
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 |
a data.frame of dummy coded variables.
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.