#' Dummify categorical features
#'
#' Generate dummy variables, i.e. One-Hot Encoding
#'
#' @param feature Vector of numbers/charactors that represent a categorical feature.
#' @param newFieldName Character string used to form name for the generated dummy variables.
#'
#' @return A data frame with each column representing a generated dummy variable.
#'
#' @export
#'
#' @examples
#' dummyMMID_UsedPlat <- dummify(feature = Data_UsedPlat$MMID, newFieldName = 'MMID')
dummify <- function(feature, newFieldName) {
cats <- unique(feature)
dummies <- as.data.frame( matrix( rep(0, length(feature) * length(cats)),
nrow = length(feature) ) )
for (i in seq_along(cats)) {
dummies[, i] <- ifelse(feature == cats[i], 1, 0)
names(dummies)[i] <- paste0("dummy", newFieldName, "_", cats[i])
}
return(dummies)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.