Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keep_original_cols() to all steps #1167

Merged
merged 4 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

* `step_bs()` and `step_ns()` have gained `keep_original_cols` argument.

* The `keep_original_cols` argument has been added to `step_classdist()`, `step_count()`, `step_depth()`, `step_geodist()`, `step_indicate_na()`, `step_interact()`, `step_lag()`, `step_poly()`, `step_regex()`, `step_window()`. The default for each step is set to preserve past behavior. This change should mean that every step that produces new columns has the `keep_original_cols` argument.

# recipes 1.0.6

## Improvements
Expand Down
9 changes: 7 additions & 2 deletions R/classdist.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ step_classdist <- function(recipe,
log = TRUE,
objects = NULL,
prefix = "classdist_",
keep_original_cols = TRUE,
skip = FALSE,
id = rand_id("classdist")) {
if (!is.character(class) || length(class) != 1) {
Expand All @@ -97,6 +98,7 @@ step_classdist <- function(recipe,
log = log,
objects = objects,
prefix = prefix,
keep_original_cols = keep_original_cols,
skip = skip,
id = id,
case_weights = NULL
Expand All @@ -105,8 +107,8 @@ step_classdist <- function(recipe,
}

step_classdist_new <-
function(terms, class, role, trained, mean_func,
cov_func, pool, log, objects, prefix, skip, id, case_weights) {
function(terms, class, role, trained, mean_func, cov_func, pool, log, objects,
prefix, keep_original_cols, skip, id, case_weights) {
step(
subclass = "classdist",
terms = terms,
Expand All @@ -119,6 +121,7 @@ step_classdist_new <-
log = log,
objects = objects,
prefix = prefix,
keep_original_cols = keep_original_cols,
skip = skip,
id = id,
case_weights = case_weights
Expand Down Expand Up @@ -199,6 +202,7 @@ prep.step_classdist <- function(x, training, info = NULL, ...) {
log = x$log,
objects = res,
prefix = x$prefix,
keep_original_cols = get_keep_original_cols(x),
skip = x$skip,
id = x$id,
case_weights = were_weights_used
Expand Down Expand Up @@ -252,6 +256,7 @@ bake.step_classdist <- function(object, new_data, ...) {
newname <- paste0(object$prefix, colnames(res))
res <- check_name(res, new_data, object, newname)
res <- vec_cbind(new_data, res)
res <- remove_original_cols(res, object, x_cols)
res
}

Expand Down
8 changes: 7 additions & 1 deletion R/count.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ step_count <- function(recipe,
options = list(),
result = make.names(pattern),
input = NULL,
keep_original_cols = TRUE,
skip = FALSE,
id = rand_id("count")) {
if (!is.character(pattern)) {
Expand Down Expand Up @@ -90,14 +91,16 @@ step_count <- function(recipe,
options = options,
result = result,
input = input,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
)
}

step_count_new <-
function(terms, role, trained, pattern, normalize, options, result, input, skip, id) {
function(terms, role, trained, pattern, normalize, options, result, input,
keep_original_cols, skip, id) {
step(
subclass = "count",
terms = terms,
Expand All @@ -108,6 +111,7 @@ step_count_new <-
options = options,
result = result,
input = input,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
Expand All @@ -131,6 +135,7 @@ prep.step_count <- function(x, training, info = NULL, ...) {
options = x$options,
input = col_name,
result = x$result,
keep_original_cols = get_keep_original_cols(x),
skip = x$skip,
id = x$id
)
Expand Down Expand Up @@ -164,6 +169,7 @@ bake.step_count <- function(object, new_data, ...) {
totals <- nchar(as.character(new_data[[object$input]]))
new_data[, object$result] <- new_data[, object$result] / totals
}
new_data <- remove_original_cols(new_data, object, names(object$input))
new_data
}

Expand Down
7 changes: 6 additions & 1 deletion R/depth.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ step_depth <-
options = list(),
data = NULL,
prefix = "depth_",
keep_original_cols = TRUE,
skip = FALSE,
id = rand_id("depth")) {
if (!is.character(class) || length(class) != 1) {
Expand All @@ -108,6 +109,7 @@ step_depth <-
options = options,
data = data,
prefix = prefix,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
Expand All @@ -116,7 +118,7 @@ step_depth <-

step_depth_new <-
function(terms, class, role, trained, metric,
options, data, prefix, skip, id) {
options, data, prefix, keep_original_cols, skip, id) {
step(
subclass = "depth",
terms = terms,
Expand All @@ -127,6 +129,7 @@ step_depth_new <-
options = options,
data = data,
prefix = prefix,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
Expand All @@ -150,6 +153,7 @@ prep.step_depth <- function(x, training, info = NULL, ...) {
options = x$options,
data = x_dat,
prefix = x$prefix,
keep_original_cols = get_keep_original_cols(x),
skip = x$skip,
id = x$id
)
Expand Down Expand Up @@ -191,6 +195,7 @@ bake.step_depth <- function(object, new_data, ...) {
newname <- paste0(object$prefix, colnames(res))
res <- check_name(res, new_data, object, newname)
res <- vec_cbind(new_data, res)
res <- remove_original_cols(res, object, x_names)
res
}

Expand Down
8 changes: 6 additions & 2 deletions R/geodist.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ step_geodist <- function(recipe,
log = FALSE,
name = "geo_dist",
columns = NULL,
keep_original_cols = TRUE,
skip = FALSE,
id = rand_id("geodist")) {
if (length(ref_lon) != 1 || !is.numeric(ref_lon)) {
Expand Down Expand Up @@ -101,6 +102,7 @@ step_geodist <- function(recipe,
log = log,
name = name,
columns = columns,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
Expand All @@ -109,7 +111,7 @@ step_geodist <- function(recipe,

step_geodist_new <-
function(lon, lat, role, trained, ref_lon, ref_lat, is_lat_lon,
log, name, columns, skip, id) {
log, name, columns, keep_original_cols, skip, id) {
step(
subclass = "geodist",
lon = lon,
Expand All @@ -122,6 +124,7 @@ step_geodist_new <-
log = log,
name = name,
columns = columns,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
Expand Down Expand Up @@ -165,6 +168,7 @@ prep.step_geodist <- function(x, training, info = NULL, ...) {
log = x$log,
name = x$name,
columns = c(lat_name, lon_name),
keep_original_cols = get_keep_original_cols(x),
skip = x$skip,
id = x$id
)
Expand Down Expand Up @@ -243,7 +247,7 @@ bake.step_geodist <- function(object, new_data, ...) {
geo_data <- check_name(geo_data, new_data, object, newname = object$name)

new_data <- vec_cbind(new_data, geo_data)

new_data <- remove_original_cols(new_data, object, names(object$columns))
new_data
}

Expand Down
8 changes: 7 additions & 1 deletion R/indicate_na.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ step_indicate_na <-
trained = FALSE,
columns = NULL,
prefix = "na_ind",
keep_original_cols = TRUE,
skip = FALSE,
id = rand_id("indicate_na")) {
terms <- enquos(...)
Expand All @@ -60,21 +61,24 @@ step_indicate_na <-
trained = trained,
columns = columns,
prefix = prefix,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
)
}

step_indicate_na_new <-
function(terms, role, trained, columns, prefix, skip, id) {
function(terms, role, trained, columns, prefix, keep_original_cols, skip,
id) {
step(
subclass = "indicate_na",
terms = terms,
role = role,
trained = trained,
columns = columns,
prefix = prefix,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
Expand All @@ -90,6 +94,7 @@ prep.step_indicate_na <- function(x, training, info = NULL, ...) {
trained = TRUE,
columns = col_names,
prefix = x$prefix,
keep_original_cols = get_keep_original_cols(x),
skip = x$skip,
id = x$id
)
Expand All @@ -112,6 +117,7 @@ bake.step_indicate_na <- function(object, new_data, ...) {
cols <- check_name(cols, new_data, object, names(cols))

new_data <- vec_cbind(new_data, cols)
new_data <- remove_original_cols(new_data, object, names(object$columns))
new_data
}

Expand Down
8 changes: 7 additions & 1 deletion R/interact.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ step_interact <-
trained = FALSE,
objects = NULL,
sep = "_x_",
keep_original_cols = TRUE,
skip = FALSE,
id = rand_id("interact")) {
add_step(
Expand All @@ -97,6 +98,7 @@ step_interact <-
role = role,
objects = objects,
sep = sep,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
Expand All @@ -105,14 +107,15 @@ step_interact <-

## Initializes a new object
step_interact_new <-
function(terms, role, trained, objects, sep, skip, id) {
function(terms, role, trained, objects, sep, keep_original_cols, skip, id) {
step(
subclass = "interact",
terms = terms,
role = role,
trained = trained,
objects = objects,
sep = sep,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
Expand All @@ -133,6 +136,7 @@ prep.step_interact <- function(x, training, info = NULL, ...) {
trained = TRUE,
objects = x$objects,
sep = x$sep,
keep_original_cols = x$keep_original_cols,
skip = x$skip,
id = x$id
)
Expand Down Expand Up @@ -211,6 +215,7 @@ prep.step_interact <- function(x, training, info = NULL, ...) {
trained = TRUE,
objects = int_terms,
sep = x$sep,
keep_original_cols = get_keep_original_cols(x),
skip = x$skip,
id = x$id
)
Expand Down Expand Up @@ -262,6 +267,7 @@ bake.step_interact <- function(object, new_data, ...) {
out <- as_tibble(out)
out <- check_name(out, new_data, object, names(out))
new_data <- vec_cbind(new_data, out)
new_data <- remove_original_cols(new_data, object, col_names)
new_data
}

Expand Down
8 changes: 7 additions & 1 deletion R/lag.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ step_lag <-
prefix = "lag_",
default = NA,
columns = NULL,
keep_original_cols = TRUE,
skip = FALSE,
id = rand_id("lag")) {
add_step(
Expand All @@ -63,14 +64,16 @@ step_lag <-
default = default,
prefix = prefix,
columns = columns,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
)
}

step_lag_new <-
function(terms, role, trained, lag, default, prefix, columns, skip, id) {
function(terms, role, trained, lag, default, prefix, columns,
keep_original_cols, skip, id) {
step(
subclass = "lag",
terms = terms,
Expand All @@ -80,6 +83,7 @@ step_lag_new <-
default = default,
prefix = prefix,
columns = columns,
keep_original_cols = keep_original_cols,
skip = skip,
id = id
)
Expand All @@ -95,6 +99,7 @@ prep.step_lag <- function(x, training, info = NULL, ...) {
default = x$default,
prefix = x$prefix,
columns = recipes_eval_select(x$terms, training, info),
keep_original_cols = get_keep_original_cols(x),
skip = x$skip,
id = x$id
)
Expand Down Expand Up @@ -124,6 +129,7 @@ bake.step_lag <- function(object, new_data, ...) {
calls <- check_name(calls, new_data, object, newname, TRUE)

new_data <- mutate(new_data, !!!calls)
new_data <- remove_original_cols(new_data, object, names(object$columns))
new_data
}

Expand Down
Loading
Loading