-
Notifications
You must be signed in to change notification settings - Fork 1
New Function Addition: Yes/No Conversion #3
Description
There is no function in R that allows one to convert a Yes/No field into either a logical (TRUE/FALSE) or as a numeric factor (1,0). It is proposed that a function be created that achieves this objective.
Justification: Many times when working with machine learning binary models where predictive outputs are being analyzed, a yes or a no option may be encoded. If this occurs, encoding to either binary or logical values may be required. A function to accelerate this effort would make working with binary predictive data in machine learning models more convenient.
Proposed Function Name: conv_yn
Function Body: conv_yn(ds, fldname, out = a, type = Bin)
Argument List Configuration:
ds = Data Frame Object
fldname = Target Field Name
out = Output Action: options are c = convert, append = a, vector = v
type = Bin or Log
Argument Explanation:
ds and fldname are self-explanatory.
out - The out argument allows the user to choice between 1 of 3 options for how the output will be configured. c converts the target variable to either a logical or a numeric output, a appends the target output as a separate variable to the passed dataset, or v converts the output to a separate vector. The vector name should use the name of the passed variable + vec for "vector." For example, if the passed variable was "buy", and v was the out option, the vector would be named "buy_v."
type - This argument determines how the output will be encoded both as data and as class. Two options are available here called Bin or Log. Bin will encode the variable as class factor with output being 1/0. Log encodes the variable as class logical with an output of TRUE/FALSE.
Function Development Considerations:
Before the function executes a conversion, two validation rules must be observed and encoded within the function:
- The passed variable must be encoded as Yes/No values
- The function should be able to accept a passed variable of either class character or class factor
Function Default Recommendations:
The recommendation for the default action used in the out argument is a for append
The recommendation for the default action used in the type argument is Bin
Other:
The following code is recommended as part of the function that could be used:
v = as.factor(ifelse(fldname == "yes", 1,0))
v = as.logical(ifelse(fldname == "yes", 1,0))