Skip to content

Commit

Permalink
data_rename(): "Error: Following variable(s) were not found" in ver…
Browse files Browse the repository at this point in the history
…sion '0.13.0.17' (#572)

* `data_rename()`: "Error: Following variable(s) were not found" in version '0.13.0.17'
Fixes #571

* version

* comment

* styler, lintr

* docs

* no lists

* Update tests/testthat/test-data_rename.R

Co-authored-by: Etienne Bacher <[email protected]>

* fix docs

* fix

* shorter if-condition

* fix

---------

Co-authored-by: Etienne Bacher <[email protected]>
  • Loading branch information
strengejacke and etiennebacher authored Dec 13, 2024
1 parent af076d2 commit a4a8308
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 33 deletions.
4 changes: 2 additions & 2 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
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
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.

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 a4a8308

Please sign in to comment.