dummy_rows {fastDummies}R Documentation

Fast creation of dummy rows

Description

dummy_rows() quickly creates dummy rows to fill in missing rows based on all combinations of available character, factor, and date columns (if not otherwise specified). This is useful for creating balanced panel data. Columns that are not character, factor, or dates are filled in with NA (or whatever value you specify).

Usage

dummy_rows(
  .data,
  select_columns = NULL,
  dummy_value = NA,
  dummy_indicator = FALSE
)

Arguments

.data

An object with the data set you want to make dummy columns from.

select_columns

If NULL (default), uses all character, factor, and Date columns to produce categories to make the dummy rows by. If not NULL, you manually enter a string or vector of strings of columns name(s).

dummy_value

Value of the row for columns that are not selected. Default is a value of NA.

dummy_indicator

Adds binary column to say if row is dummy or not (i.e. included in original data or not)

Value

A data.frame (or tibble or data.table, depending on input data type) with same number of columns as inputted data and original rows plus the newly created dummy rows

See Also

dummy_cols For creating dummy columns

Other dummy functions: dummy_cols(), dummy_columns()

Examples

crime <- data.frame(city = c("SF", "SF", "NYC"),
    year = c(1990, 2000, 1990),
    crime = 1:3)

dummy_rows(crime)
# Include year column
dummy_rows(crime, select_columns = c("city", "year"))
# m=Make dummy value 0
dummy_rows(crime, select_columns = c("city", "year"),
    dummy_value = 0)
# Add a dummy indicator
dummy_rows(crime, select_columns = c("city", "year"),
    dummy_indicator = TRUE)

[Package fastDummies version 1.7.3 Index]