Description Usage Arguments Functions Examples
This is the reverse operation of using model.matrix
a
factor. ind_to_char
works like tidyr::unite
, it combines
multiple indicator columns into one character/factor column and add it to
the data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
data |
A |
col |
Name of the generated column. Use a bare name when using NSE functions and a character (quoted) name when using SE functions (functions that end with underscores). |
... |
Specification of indicator columns. Use bare variable names.
Select all variables between |
ret_factor |
Whether to convert the column into factor. |
remove |
If |
mutually_exclusive |
Check if the indicators are mutually exclusive. |
collectively_exhaustive |
Check if the indicators are collectively exhaustive. |
from |
Names of existing columns as character vector |
ind_to_char_
: SE version of ind_to_char
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | # Supports converting the following atomic types to indicator
df <- data.frame(integer_ind = c(2L, 0L, 0L, 0L, 0L, 0L),
# non-zero integer is 1, otherwise 0.
logcal_ind = c(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE),
# TRUE is 1.
double_ind = c(0, 0, 2.0, 0, 0, 0),
# non-zero double is 1.
char_ind = c("FALSE", "FALSE", "F", "TRUE", "T", "FALSE"),
# "T" and "TRUE" converts to 1.
factor_ind = factor(c(1, 1, 1, 1, 1, 0), levels = c(0, 1),
labels = c(TRUE, FALSE)),
# Factors are converted based on levels.
stringsAsFactors = FALSE)
ind_to_char_(df, col = "new_y", from = names(df), remove = FALSE)
# ind_to_char as complement to use model.matrix on a factor
df <- data.frame(x = 1:6, y = factor(c(letters[1:5], NA)))
ind_df <- as.data.frame(model.matrix(~ x + y - 1,
model.frame(df, na.action = na.pass)))
ind_df # an indicator matrix with NAs
# New character column is generated with non-selected columns kept as is.
ind_to_char(ind_df, new_y, ya:ye)
ind_to_char(ind_df, new_y, -x)
ind_to_char(ind_df, col = new_y, ya:ye, remove = FALSE)
# Returns a factor column
ind_to_char(ind_df, col = new_y, ya:ye, ret_factor = TRUE)
# Using SE
ind_to_char_(ind_df, col = "new_y", from = c("ya", "yb", "yc", "yd", "ye"))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.