From 29125523fd5995947eb91c8464a27ef80465614d Mon Sep 17 00:00:00 2001 From: Kevin Cazelles Date: Fri, 21 May 2021 14:44:38 -0400 Subject: [PATCH] :sparkles: rename --- DESCRIPTION | 2 +- NAMESPACE | 1 + NEWS.md | 7 ++++--- R/rename.R | 27 +++++++++++++++++++++++++++ man/rename.Rd | 23 +++++++++++++++++++++++ tests/testthat/test-rename.R | 13 +++++++++++++ 6 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 R/rename.R create mode 100644 man/rename.Rd create mode 100644 tests/testthat/test-rename.R diff --git a/DESCRIPTION b/DESCRIPTION index 228d84d..b16cc4a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: inSilecoMisc Title: inSileco Miscellaneous Functions -Date: 2020-11-22 +Date: 2021-05-21 Version: 0.6.0.9000 Authors@R: c( person("Kevin", "Cazelles", email = "kevin.cazelles@gmail.com", comment = c(ORCID = "0000-0001-6619-9874"), role = c("aut", "cre")), diff --git a/NAMESPACE b/NAMESPACE index 104f2f9..03645cc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -34,6 +34,7 @@ export(msgWarning) export(multiMatch) export(packagesUsed) export(readYamlHeader) +export(rename) export(scaleWithin) export(seqCol) export(seqRg) diff --git a/NEWS.md b/NEWS.md index 91b3877..497649f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,9 @@ # inSilecoMisc (devel) -* New function `commaAnd()` to collpase elements of a vector and add element separators. +* New function `rename()` to rename data frames, list, etc. +* New function `commaAnd()` to collapse elements of a vector and add element separators. * Add code of conduct. -* Add contributing. +* Add contributing guidelines. # inSilecoMisc 0.6.0 @@ -18,7 +19,7 @@ data frame according to the template (see #20). * add `stopwatch()` and `timer()`. * add `seqRg()` that generates a regular sequence based on the range of a vector. -* add `seqRow()` and ``seqCol()` that generate a regular sequences based on the number of rows and columns (respectively) of a data frame (or a matrix). +* add `seqRow()` and `seqCol()` that generate a regular sequences based on the number of rows and columns (respectively) of a data frame (or a matrix). # inSilecoMisc 0.4.0 diff --git a/R/rename.R b/R/rename.R new file mode 100644 index 0000000..b49900d --- /dev/null +++ b/R/rename.R @@ -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 +} + diff --git a/man/rename.Rd b/man/rename.Rd new file mode 100644 index 0000000..d151369 --- /dev/null +++ b/man/rename.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/rename.R +\name{rename} +\alias{rename} +\title{Rename object} +\usage{ +rename(x, old, new) +} +\arguments{ +\item{x}{an object from which names can be extracted see \code{\link[=names]{names()}}.} + +\item{old}{a vector of character strings of old names.} + +\item{new}{a vector of character strings of new names.} +} +\description{ +This function finds old names and replace them with new ones. +} +\examples{ +tb <- data.frame(var1 = 2, var2 = "B") +rename(tb, "var1", "uptake") +rename(tb, c("var1", "var2"), c("uptake", "type")) +} diff --git a/tests/testthat/test-rename.R b/tests/testthat/test-rename.R new file mode 100644 index 0000000..d1af6dc --- /dev/null +++ b/tests/testthat/test-rename.R @@ -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"))) +}) +