get_dummies.: Convert character and factor columns to dummy variables

Description Usage Arguments Examples

View source: R/get_dummies.R

Description

Convert character and factor columns to dummy variables

Usage

1
2
3
4
5
6
7
8
get_dummies.(
  .df,
  cols = c(where(is.character), where(is.factor)),
  prefix = TRUE,
  prefix_sep = "_",
  drop_first = FALSE,
  dummify_na = TRUE
)

Arguments

.df

A data.frame or data.table

cols

A single column or a vector of unquoted columns to dummify. Defaults to all character & factor columns using c(where(is.character), where(is.factor)). tidyselect compatible.

prefix

TRUE/FALSE - If TRUE, a prefix will be added to new column names

prefix_sep

Separator for new column names

drop_first

TRUE/FALSE - If TRUE, the first dummy column will be dropped

dummify_na

TRUE/FALSE - If TRUE, NAs will also get dummy columns

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
test_df <- tidytable(
  col1 = c("a", "b", "c", NA),
  col2 = as.factor(c("a", "b", NA, "d")),
  var1 = rnorm(4,0,1))

# Automatically does all character/factor columns
test_df %>%
  get_dummies.()

# Can select one column
test_df %>%
  get_dummies.(col1)

# Can select one or multiple columns in a vector of unquoted column names
test_df %>%
  get_dummies.(c(col1, col2))

# Can drop certain columns using
test_df %>%
  get_dummies.(c(where(is.character), -col2))

test_df %>%
  get_dummies.(prefix_sep = ".", drop_first = TRUE)

test_df %>%
  get_dummies.(c(col1, col2), dummify_na = FALSE)

mtfairbanks/gdt documentation built on May 4, 2021, 9:44 a.m.