Skip to content

Commit

Permalink
data_rename: mention use of unlist() for named lists (#580)
Browse files Browse the repository at this point in the history
* `data_rename`: mention use of `unlist()` for named lists

* style

* lintr

---------

Co-authored-by: Daniel <[email protected]>
  • Loading branch information
etiennebacher and strengejacke authored Jan 4, 2025
1 parent 416c1b0 commit 1cf5a4a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 deletions.
5 changes: 3 additions & 2 deletions R/data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@
#'
#' @details
#' `select` can also be a named character vector. In this case, the names are
#' used to rename the columns in the output data frame. See 'Examples'.
#' used to rename the columns in the output data frame. If you have a named
#' list, use `unlist()` to convert it to a named vector. See 'Examples'.
#'
#' @return A modified data frame.
#'
#' @examples
#' # Rename columns
#' head(data_rename(iris, "Sepal.Length", "length"))
#'
#' # use named vector to rename
#' # Use named vector to rename
#' head(data_rename(iris, c(length = "Sepal.Length", width = "Sepal.Width")))
#'
#' # Change all
Expand Down
2 changes: 0 additions & 2 deletions R/data_xtabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ format.datawizard_crosstab <- function(x,
}



# print, datawizard_crosstab ---------------------


Expand Down Expand Up @@ -363,7 +362,6 @@ print_html.datawizard_crosstabs <- function(x, big_mark = NULL, ...) {
}



# helper ---------------------


Expand Down
1 change: 0 additions & 1 deletion R/select_nse.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@
matches,
verbose,
ifnotfound) {

msg <- paste0(
"Following variable(s) were not found: ",
toString(x[is.na(matches)])
Expand Down
16 changes: 8 additions & 8 deletions R/smoothness.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ smoothness.numeric <- function(x,
}

if (method == "cor") {
smooth <- stats::cor(utils::head(x, length(x) - lag), utils::tail(x, length(x) - lag))
smooth_data <- stats::cor(utils::head(x, length(x) - lag), utils::tail(x, length(x) - lag))
} else {
smooth <- stats::sd(diff(x, lag = lag)) / abs(mean(diff(x, lag = lag)))
smooth_data <- stats::sd(diff(x, lag = lag)) / abs(mean(diff(x, lag = lag)))
}

if (!is.null(iterations)) {
if (!requireNamespace("boot", quietly = TRUE)) {
insight::format_warning("Package 'boot' needed for bootstrapping SEs.")
} else {
if (requireNamespace("boot", quietly = TRUE)) {
results <- boot::boot(
data = x,
statistic = .boot_smoothness,
Expand All @@ -56,12 +54,14 @@ smoothness.numeric <- function(x,
lag = lag
)
out_se <- stats::sd(results$t, na.rm = TRUE)
smooth <- data.frame(Smoothness = smooth, SE = out_se)
smooth_data <- data.frame(Smoothness = smooth_data, SE = out_se)
} else {
insight::format_warning("Package 'boot' needed for bootstrapping SEs.")
}
}

class(smooth) <- unique(c("parameters_smoothness", class(smooth)))
smooth
class(smooth_data) <- unique(c("parameters_smoothness", class(smooth_data)))
smooth_data
}


Expand Down
24 changes: 12 additions & 12 deletions R/standardize.models.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ standardize.default <- function(x,
update_expr,

Check warning on line 107 in R/standardize.models.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/standardize.models.R,line=107,col=33,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
...) {
m_info <- .get_model_info(x, ...)
data <- insight::get_data(x, source = "mf", verbose = FALSE)
model_data <- insight::get_data(x, source = "mf", verbose = FALSE)

if (isTRUE(attr(data, "is_subset"))) {
if (isTRUE(attr(model_data, "is_subset"))) {
insight::format_error("Cannot standardize a model fit with a 'subset = '.")
}

Expand Down Expand Up @@ -164,10 +164,10 @@ standardize.default <- function(x,
weight_variable <- insight::find_weights(x)

if (!is.null(weight_variable) &&
!weight_variable %in% colnames(data) &&
"(weights)" %in% colnames(data)) {
data$.missing_weight <- data[["(weights)"]]
colnames(data)[ncol(data)] <- weight_variable
!weight_variable %in% colnames(model_data) &&
"(weights)" %in% colnames(model_data)) {
model_data$.missing_weight <- model_data[["(weights)"]]
colnames(model_data)[ncol(model_data)] <- weight_variable
weight_variable <- c(weight_variable, "(weights)")
}

Expand All @@ -178,12 +178,12 @@ standardize.default <- function(x,
## ---- SUMMARY: TO Z OR NOT TO Z? ----

dont_standardize <- c(resp, weight_variable, random_group_factor)
do_standardize <- setdiff(colnames(data), dont_standardize)
do_standardize <- setdiff(colnames(model_data), dont_standardize)

# can't std data$var variables
doller_vars <- grepl("(.*)\\$(.*)", do_standardize)
if (any(doller_vars)) {
doller_vars <- colnames(data)[doller_vars]
doller_vars <- colnames(model_data)[doller_vars]
insight::format_warning(
"Unable to standardize variables evaluated in the environment (i.e., not in `data`).",
"The following variables will not be standardizd:",
Expand All @@ -204,7 +204,7 @@ standardize.default <- function(x,

w <- insight::get_weights(x, remove_na = TRUE)

data_std <- standardize(data[do_standardize],
data_std <- standardize(model_data[do_standardize],
robust = robust,
two_sd = two_sd,
weights = if (weights) w,
Expand All @@ -213,7 +213,7 @@ standardize.default <- function(x,

# if two_sd, it must not affect the response!
if (include_response && two_sd) {
data_std[resp] <- standardize(data[resp],
data_std[resp] <- standardize(model_data[resp],
robust = robust,
two_sd = FALSE,
weights = if (weights) w,
Expand Down Expand Up @@ -254,8 +254,8 @@ standardize.default <- function(x,

## ---- ADD BACK VARS THAT WHERE NOT Z ----
if (length(dont_standardize)) {
remaining_columns <- intersect(colnames(data), dont_standardize)
data_std <- cbind(data[, remaining_columns, drop = FALSE], data_std)
remaining_columns <- intersect(colnames(model_data), dont_standardize)
data_std <- cbind(model_data[, remaining_columns, drop = FALSE], data_std)
}


Expand Down
5 changes: 3 additions & 2 deletions man/data_rename.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1cf5a4a

Please sign in to comment.