-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kevin Cazelles
committed
May 21, 2021
1 parent
9cdf11b
commit 2912552
Showing
6 changed files
with
69 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#' Rename object | ||
#' | ||
#' This function finds old names and replace them with new ones. | ||
#' | ||
#' @param x an object from which names can be extracted see [names()]. | ||
#' @param old a vector of character strings of old names. | ||
#' @param new a vector of character strings of new names. | ||
#' | ||
#' @export | ||
#' @examples | ||
#' tb <- data.frame(var1 = 2, var2 = "B") | ||
#' rename(tb, "var1", "uptake") | ||
#' rename(tb, c("var1", "var2"), c("uptake", "type")) | ||
|
||
rename <- function(x, old, new) { | ||
stopifnot(length(new) == length(old)) | ||
out <- x | ||
for (i in seq_along(old)) { | ||
if (sum(names(out) == old[i])) { | ||
names(out)[names(out) == old[i]] <- new[i] | ||
} else { | ||
msgWarning(old[i], "not a valid name.") | ||
} | ||
} | ||
out | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
context("renaming object") | ||
|
||
tb <- data.frame(var1 = 2, var2 = "B") | ||
res1 <- rename(tb, c("var1", "var2"), c("uptake", "type")) | ||
res2 <- rename(tb, c("var"), c("cool")) | ||
|
||
|
||
test_that("renamed as exected", { | ||
expect_equal(names(res1), c("uptake", "type")) | ||
expect_equal(names(res2), names(tb)) | ||
expect_error(rename(tb, "var1", c("up", "take"))) | ||
}) | ||
|