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

loopify step_profile() #1177

Merged
merged 2 commits into from
Aug 1, 2023
Merged
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
23 changes: 15 additions & 8 deletions R/profile.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,24 @@ prep.step_profile <- function(x, training, info = NULL, ...) {

#' @export
bake.step_profile <- function(object, new_data, ...) {
n <- length(object$profile[[1]])
new_data <- new_data[rep(1, n), ]
keepers <- c(names(object$columns), names(object$profile))
# Keep the predictors in the same order
keepers <- names(new_data)[names(new_data) %in% keepers]
new_data <- dplyr::select(new_data, !!keepers)
col_names <- names(object$columns)
profile_names <- names(object$profile)
keepers <- c(col_names, profile_names)

for (i in names(object$columns)) {
new_data[[i]] <- rep(object$columns[[i]], n)
new_data <- list()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it easier to create a empty list, fill it, order it, tibblify it. Than to modify the existing tibble, order it making sure the lengths matched


for (col_name in col_names) {
new_data[[col_name]] <- object$columns[[col_name]]
}

new_data[[names(object$profile)]] <- object$profile[[1]]

new_data <- vctrs::vec_recycle_common(!!!new_data)
Copy link
Member Author

@EmilHvitfeldt EmilHvitfeldt Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is so cool! We can avoid doing rep(value, n)

aaa <- list(1, 2, 4, 1:5)

vctrs::vec_recycle_common(!!!aaa)
#> [[1]]
#> [1] 1 1 1 1 1
#> 
#> [[2]]
#> [1] 2 2 2 2 2
#> 
#> [[3]]
#> [1] 4 4 4 4 4
#> 
#> [[4]]
#> [1] 1 2 3 4 5

new_data <- tibble::new_tibble(new_data)

# Keep the predictors in the same order
keepers <- names(new_data)[names(new_data) %in% keepers]
new_data <- new_data[keepers]
new_data
}

Expand Down
Loading