Create a binary vector from numeric, date, and character or factor vectors.
Source:R/create_variables.R
dummy_num.Rd
Create a binary vector from numeric, date, and character or factor vectors.
Arguments
- dat
Primary data containing information on hauls or trips. Table in the FishSET database contains the string 'MainDataTable'.
- project
Project name.
- var
Variable in
dat
to create dummy variable from.- value
String, value to set dummy variable by. If
var
is a date, value should be a year, Ifvar
is a factor, value should be a factor level. Ifvar
is numeric, value should be a single number or range of numbers [use c(1,5)].- opts
String, how dummy variable should be defined. Choices are
"x_y"
and"more_less’"
. For"x_y"
, each element ofvar
is set to 1 if the element matchesvalue
, otherwise 0. For"more_less"
, each element ofvar
less thanvalue
is set to 0 and all elements greater thanvalue
set to 1. Ifvar
is a factor, then elements that match value will be set to 1 and all other elements set to 0. Default is set to"more_less"
.- name
String, name of created dummy variable. Defaults to name of the function if not defined.
Details
For date variables, the dummy variable is defined by a date (year) and may be either year x
versus all
other years ("x_y"
) or before vs after year x
("more_less"
). Use this function to create a variable defining whether
or not a policy action had been implemented.
Example: before vs. after a 2008 amendment: dummy_num('pollockMainDataTable', 'Haul_date', 2008, 'more_less', 'amend08')
For factor variables, both choices in opts
compare selected factor level(s) against all other factor levels.
Example: Fishers targeting pollock vs. another species: dummy_num('pollockMainDataTable', 'GF_TARGET_FT', c('Pollock - bottom', 'Pollock - midwater'), 'x_y', 'pollock_target')
For numeric variables, value
can be a single number or a range of numbers. The dummy variable is the
selected value(s) against all others (x_y
) or less than the selected value versus more than the selected value
(more_less
). For more_less
, the mean is used as the critical value if a range of values is provided.