Skip to content

Commit 4921adb

Browse files
committed
one_hot_encoder() can return the new variables as integer using the as_integer parameter
1 parent e403639 commit 4921adb

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

R/one_hot_encoder.R

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#' One-hot Encoder
22
#'
33
#' @param data Input data frame
4-
#' @param feature Unquoted of the feature/column to encode
4+
#' @param feature Unquoted form of the feature/column to encode
5+
#' @param as_integer Wether the boolean should be converted to integer or not
56
#'
67
#' @return
78
#' @export
89
#'
910
#' @examples
1011
#' set.seed(11)
1112
#' one_hot_encoder(iris[sample(1:150, 10),], Species)
12-
one_hot_encoder <- function(data, feature) {
13+
one_hot_encoder <- function(data, feature, as_integer = FALSE) {
1314

1415
# Deparse feature name
1516
feature_name <- deparse(substitute(feature))
@@ -44,5 +45,13 @@ one_hot_encoder <- function(data, feature) {
4445
data[[labels[i]]] <- stringr::str_detect(processed_feature, detection_labels[i])
4546
}
4647

48+
if (as_integer) {
49+
data <- data %>%
50+
dplyr::mutate_at(
51+
.vars = dplyr::vars(labels),
52+
.funs = as.integer
53+
)
54+
}
55+
4756
return(data %>% dplyr::select(-{{ feature }}))
4857
}

man/one_hot_encoder.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)