ind_to_char: Convert indicator data.frame to character/factor.

Description Usage Arguments Functions Examples

View source: R/grp_routine.R

Description

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.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
ind_to_char(
  data,
  col,
  ...,
  ret_factor = FALSE,
  remove = TRUE,
  mutually_exclusive = TRUE,
  collectively_exhaustive = TRUE
)

ind_to_char_(
  data,
  col,
  from,
  ret_factor = FALSE,
  remove = TRUE,
  mutually_exclusive = TRUE,
  collectively_exhaustive = TRUE
)

Arguments

data

A data.frame or tbl.

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 x and z with x:z. For more options, see the select documentation.

ret_factor

Whether to convert the column into factor.

remove

If TRUE, remove input column from output data frame.

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

Functions

Examples

 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"))

extdplyr documentation built on April 20, 2020, 9:04 a.m.