Skip to content

Commit

Permalink
Merge branch 'main' into bump-version-requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke authored Dec 13, 2024
2 parents 741f35c + a4a8308 commit 0e90e9c
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 40 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.13.0.17
Version: 0.13.0.18
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531")),
Expand Down
12 changes: 4 additions & 8 deletions R/adjust.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#'
#' @return A data frame comparable to `data`, with adjusted variables.
#'
#' @examplesIf require("bayestestR", quietly = TRUE) && require("rstanarm", quietly = TRUE) && require("gamm4", quietly = TRUE)
#' @examplesIf all(insight::check_if_installed(c("bayestestR", "rstanarm", "gamm4"), quietly = TRUE))
#' adjusted_all <- adjust(attitude)
#' head(adjusted_all)
#' adjusted_one <- adjust(attitude, effect = "complaints", select = "rating")
Expand All @@ -43,7 +43,7 @@
#' }
#'
#' # Generate data
#' data <- simulate_correlation(n = 100, r = 0.7)
#' data <- bayestestR::simulate_correlation(n = 100, r = 0.7)
#' data$V2 <- (5 * data$V2) + 20 # Add intercept
#'
#' # Adjust
Expand Down Expand Up @@ -75,12 +75,8 @@ adjust <- function(data,
ignore_case = FALSE,
regex = FALSE,
verbose = FALSE) {
if (!all(colnames(data) == make.names(colnames(data), unique = TRUE))) {
insight::format_warning(
"Bad column names (e.g., with spaces) have been detected which might create issues in many functions.",
"Please fix it (you can run `names(mydata) <- make.names(names(mydata))` for a quick fix)."
)
}
# make sure column names are syntactically valid
.check_dataframe_names(data, action = "error")

# check for formula notation, convert to character vector
if (inherits(effect, "formula")) {
Expand Down
7 changes: 6 additions & 1 deletion R/data_rename.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#' @title Rename columns and variable names
#' @name data_rename
#'
#' @description Safe and intuitive functions to rename variables or rows in
#' data frames. `data_rename()` will rename column names, i.e. it facilitates
Expand All @@ -8,7 +9,7 @@
#' possible pipe-workflow.
#'
#' @inheritParams extract_column_names
#' @param data A data frame, or an object that can be coerced to a data frame.
#' @param data A data frame.
#' @param replacement Character vector. Can be one of the following:
#' - A character vector that indicates the new names of the columns selected
#' in `select`. `select` and `replacement` must be of the same length.
Expand Down Expand Up @@ -89,6 +90,10 @@ data_rename <- function(data,
verbose = TRUE,
pattern = NULL,
...) {
# check for valid input
if (!is.data.frame(data)) {
insight::format_error("Argument `data` must be a data frame.")
}
# If the user does data_rename(iris, pattern = "Sepal.Length", "length"),
# then "length" is matched to select by position while it's the replacement
# => do the switch manually
Expand Down
18 changes: 8 additions & 10 deletions R/data_rescale.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#' Rescale Variables to a New Range
#' @title Rescale Variables to a New Range
#' @name rescale
#'
#' @description
#' Rescale variables to a new range. Can also be used to reverse-score variables
#' (change the keying/scoring direction), or to expand a range.
#'
#' @inheritParams categorize
#' @inheritParams extract_column_names
#' @inheritParams standardize.data.frame
#'
#' @param to Numeric vector of length 2 giving the new range that the variable
#' will have after rescaling. To reverse-score a variable, the range should
#' be given with the maximum value first. See examples.
Expand All @@ -27,6 +28,11 @@
#'
#' @inheritSection center Selection of variables - the `select` argument
#'
#' @seealso See [makepredictcall.dw_transformer()] for use in model formulas.
#' @family transform utilities
#'
#' @return A rescaled object.
#'
#' @examples
#' rescale(c(0, 1, 5, -5, -2))
#' rescale(c(0, 1, 5, -5, -2), to = c(-5, 5))
Expand Down Expand Up @@ -62,14 +68,6 @@
#' # Specify list of multipliers
#' d <- data.frame(x = 5:15, y = 5:15)
#' rescale(d, multiply = list(x = 1.1, y = 0.5))
#'
#' @inherit data_rename
#'
#' @return A rescaled object.
#'
#' @seealso See [makepredictcall.dw_transformer()] for use in model formulas.
#' @family transform utilities
#'
#' @export
rescale <- function(x, ...) {
UseMethod("rescale")
Expand Down
3 changes: 2 additions & 1 deletion R/data_rotate.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#' names.
#' @param verbose Toggle warnings.
#'
#' @inherit data_rename seealso
#'
#' @return A (rotated) data frame.
#'
#' @examples
Expand All @@ -36,7 +38,6 @@
#' data_rotate(x, colnames = TRUE)
#' data_rotate(x, colnames = "c")
#'
#' @inherit data_rename seealso
#' @export
data_rotate <- function(data, rownames = NULL, colnames = FALSE, verbose = TRUE) {
# copy attributes
Expand Down
9 changes: 6 additions & 3 deletions R/data_to_long.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#' Reshape (pivot) data from wide to long
#' @title Reshape (pivot) data from wide to long
#' @name data_to_long
#'
#' @description
#' This function "lengthens" data, increasing the number of rows and decreasing
#' the number of columns. This is a dependency-free base-R equivalent of
#' `tidyr::pivot_longer()`.
Expand Down Expand Up @@ -31,6 +33,8 @@
#' with `tidyr::pivot_longer()`. If both `select` and `cols` are provided, `cols`
#' is used.
#'
#' @inherit data_rename seealso
#'
#' @details
#' Reshaping data into long format usually means that the input data frame is
#' in _wide_ format, where multiple measurements taken on the same subject are
Expand Down Expand Up @@ -58,7 +62,7 @@
#' @return If a tibble was provided as input, `reshape_longer()` also returns a
#' tibble. Otherwise, it returns a data frame.
#'
#' @examplesIf requireNamespace("psych") && requireNamespace("tidyr")
#' @examplesIf all(insight::check_if_installed(c("psych", "tidyr"), quietly = TRUE))
#' wide_data <- setNames(
#' data.frame(replicate(2, rnorm(8))),
#' c("Time1", "Time2")
Expand Down Expand Up @@ -122,7 +126,6 @@
#' values_to = "count"
#' )
#' head(even_longer_data)
#' @inherit data_rename
#' @export
data_to_long <- function(data,
select = "all",
Expand Down
11 changes: 5 additions & 6 deletions R/select_nse.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,11 @@

# small helper, to avoid duplicated code
.action_if_not_found <- function(
x,
columns,
matches,
verbose,
ifnotfound
) {
x,
columns,
matches,
verbose,
ifnotfound) {
msg <- paste0(
"Following variable(s) were not found: ",
toString(x[is.na(matches)])
Expand Down
23 changes: 23 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@
}


#' Checks dataframes for syntactically valid column names
#' Argument "action" can be "warning", "error", or "message".
#'
#' @keywords internal
#' @noRd
.check_dataframe_names <- function(x, action = "warning", verbose = TRUE) {
if (verbose && !all(colnames(x) == make.names(colnames(x), unique = TRUE))) {
insight::format_alert(
"Bad column names (e.g., with spaces) have been detected which might create issues in many functions.",
paste0(
"We recommend to rename following columns: ",
text_concatenate(
colnames(x)[colnames(x) != make.names(colnames(x), unique = TRUE)],
enclose = "`"
)
),
"You can run `names(mydata) <- make.names(names(mydata))` or use `janitor::clean_names()` for a quick fix.", # nolint
type = action
)
}
}


#' Fuzzy grep, matches pattern that are close, but not identical
#' @examples
#' colnames(iris)
Expand Down
4 changes: 2 additions & 2 deletions man/adjust.Rd

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

2 changes: 1 addition & 1 deletion man/data_partition.Rd

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

2 changes: 1 addition & 1 deletion man/data_rename.Rd

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

2 changes: 1 addition & 1 deletion man/data_to_long.Rd

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

5 changes: 0 additions & 5 deletions man/rescale.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test-adjust.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ test_that("adjust regex", {
adjust(mtcars, select = "mpg")
)
})

# select helpers ------------------------------
test_that("adjust, invalid column names", {
data(iris)
colnames(iris)[1] <- "I am"
expect_error(
adjust(iris[c("I am", "Species")], multilevel = FALSE, bayesian = FALSE),
regex = "Bad column names"
)
})
8 changes: 8 additions & 0 deletions tests/testthat/test-data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,11 @@ test_that("Argument `pattern` is deprecated", {
fixed = TRUE
)
})

test_that("works with lists", {
result <- list(x = 1, y = 2)
expect_error(
data_rename(result, select = names(result), replacement = c("a", "b")),
regex = "must be a data frame"
)
})

0 comments on commit 0e90e9c

Please sign in to comment.