From 92e803273bc4ffd6f914ed8721534214ba7033e6 Mon Sep 17 00:00:00 2001 From: "Patrick R. Wright" Date: Tue, 31 Mar 2020 13:12:32 +0200 Subject: [PATCH 1/6] added na_to_level function --- R/na_to_level.R | 18 ++++++++++++++++++ man/na_to_level.Rd | 22 ++++++++++++++++++++++ tests/testthat/test-na_to_level.R | 10 ++++++++++ 3 files changed, 50 insertions(+) create mode 100644 R/na_to_level.R create mode 100644 man/na_to_level.Rd create mode 100644 tests/testthat/test-na_to_level.R diff --git a/R/na_to_level.R b/R/na_to_level.R new file mode 100644 index 0000000..41a13c4 --- /dev/null +++ b/R/na_to_level.R @@ -0,0 +1,18 @@ +#' This functions replaces all NA's in a factor to a level with specified name. +#' +#' @param x a factor +#' @param name character - the name of the new level (default: N/A) +#' @return The same factor with renamed NA +#' @export +#' @examples +#' na_to_level(factor(c("a","b", "b", NA))) # Levels: a b N/A +na_to_level <- function(x, name = "N/A") { + assertthat::assert_that(is.factor(x)) + + if(!(name %in% levels(x))) { + x <- factor(x, levels = c(levels(x), name)) + } + x[is.na(x)] <- name + + return(x) +} diff --git a/man/na_to_level.Rd b/man/na_to_level.Rd new file mode 100644 index 0000000..5f15078 --- /dev/null +++ b/man/na_to_level.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/na_to_level.R +\name{na_to_level} +\alias{na_to_level} +\title{This functions replaces all NA's in a factor to a level with specified name.} +\usage{ +na_to_level(x, name = "N/A") +} +\arguments{ +\item{x}{a factor} + +\item{name}{character - the name of the new level (default: N/A)} +} +\value{ +The same factor with renamed NA +} +\description{ +This functions replaces all NA's in a factor to a level with specified name. +} +\examples{ +na_to_level(factor(c("a","b", "b", NA))) # Levels: a b N/A +} diff --git a/tests/testthat/test-na_to_level.R b/tests/testthat/test-na_to_level.R new file mode 100644 index 0000000..30b5751 --- /dev/null +++ b/tests/testthat/test-na_to_level.R @@ -0,0 +1,10 @@ +context("na to level") + +test_that("Test fail", { + expect_error(na_to_level(123)) + expect_error(na_to_level("abc")) +}) + +test_that("Test output", { + expect_equal(na_to_level(factor(c("a","b", "b", NA))), factor(c("a","b", "b", "N/A"))) +}) \ No newline at end of file From 3edf8af5a1e8be9903be9094c470a7640e3d9d47 Mon Sep 17 00:00:00 2001 From: "Patrick R. Wright" Date: Tue, 31 Mar 2020 13:12:55 +0200 Subject: [PATCH 2/6] update documentation --- DESCRIPTION | 2 +- NAMESPACE | 1 + NEWS.md | 3 +++ README.md | 2 +- 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 306389e..879d805 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: CTUHelpR Title: What the Package Does (One Line, Title Case) -Version: 0.0.1 +Version: 0.0.2 Author: c(person(given = "First", family = "Last", diff --git a/NAMESPACE b/NAMESPACE index c169543..e1fc7d8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,5 @@ # Generated by roxygen2: do not edit by hand +export(na_to_level) export(print_file_content) importFrom(readr,read_file) diff --git a/NEWS.md b/NEWS.md index a70f97c..5d00ea1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,2 +1,5 @@ +# CTUHelpR 0.0.2 +* the `na_to_level()` function has been added to the package + # CTUHelpR 0.0.1 * initialize `CTUHelpR` diff --git a/README.md b/README.md index 22681c8..f28eb5a 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ -# CTUHelpR ![travis](https://api.travis-ci.com/CTU-Basel/CTUHelpR.svg?branch=master) [![codecov](https://codecov.io/github/CTU-Basel/CTUHelpR/branch/master/graphs/badge.svg)](https://codecov.io/github/CTU-Basel/CTUHelpR) [![](https://img.shields.io/badge/dev%20version-0.0.1-blue.svg)](https://github.com/CTU-Basel/CTUHelpR) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/CTU-Basel/CTUHelpR?branch=master&svg=true)](https://ci.appveyor.com/project/CTU-Basel/CTUHelpR) +# CTUHelpR ![travis](https://api.travis-ci.com/CTU-Basel/CTUHelpR.svg?branch=master) [![codecov](https://codecov.io/github/CTU-Basel/CTUHelpR/branch/master/graphs/badge.svg)](https://codecov.io/github/CTU-Basel/CTUHelpR) [![](https://img.shields.io/badge/dev%20version-0.0.2-blue.svg)](https://github.com/CTU-Basel/CTUHelpR) [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/CTU-Basel/CTUHelpR?branch=master&svg=true)](https://ci.appveyor.com/project/CTU-Basel/CTUHelpR) ## Installing from github with devtools From 8185e2597ff35528d6c022da2ab087af1178ffe9 Mon Sep 17 00:00:00 2001 From: "Patrick R. Wright" Date: Tue, 31 Mar 2020 13:17:30 +0200 Subject: [PATCH 3/6] add dependency handling assertthat --- R/na_to_level.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/na_to_level.R b/R/na_to_level.R index 41a13c4..f596ab4 100644 --- a/R/na_to_level.R +++ b/R/na_to_level.R @@ -3,11 +3,12 @@ #' @param x a factor #' @param name character - the name of the new level (default: N/A) #' @return The same factor with renamed NA +#' @importFrom assertthat assert_that #' @export #' @examples #' na_to_level(factor(c("a","b", "b", NA))) # Levels: a b N/A na_to_level <- function(x, name = "N/A") { - assertthat::assert_that(is.factor(x)) + assert_that(is.factor(x)) if(!(name %in% levels(x))) { x <- factor(x, levels = c(levels(x), name)) From 289dcb976c424287f3e79430f3f8f9058c44e66c Mon Sep 17 00:00:00 2001 From: "Patrick R. Wright" Date: Tue, 31 Mar 2020 13:17:44 +0200 Subject: [PATCH 4/6] add dependency handling assertthat --- NAMESPACE | 1 + 1 file changed, 1 insertion(+) diff --git a/NAMESPACE b/NAMESPACE index e1fc7d8..732ace1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,4 +2,5 @@ export(na_to_level) export(print_file_content) +importFrom(assertthat,assert_that) importFrom(readr,read_file) From 5ad2c5abccd3f4c3c6448374a353d69fb57e521e Mon Sep 17 00:00:00 2001 From: "Patrick R. Wright" Date: Tue, 31 Mar 2020 13:18:50 +0200 Subject: [PATCH 5/6] add dependency handling assertthat --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 879d805..07beef1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,7 +19,8 @@ RoxygenNote: 7.0.2 Depends: R (>= 3.5) Imports: - readr + readr, + assertthat Suggests: knitr, lintr, From 2c197f78b37bea941cb77f20a60ecea652145fc1 Mon Sep 17 00:00:00 2001 From: "Patrick R. Wright" Date: Tue, 31 Mar 2020 13:38:12 +0200 Subject: [PATCH 6/6] test fix appveyor --- tests/testthat/test-na_to_level.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-na_to_level.R b/tests/testthat/test-na_to_level.R index 30b5751..5346943 100644 --- a/tests/testthat/test-na_to_level.R +++ b/tests/testthat/test-na_to_level.R @@ -6,5 +6,7 @@ test_that("Test fail", { }) test_that("Test output", { - expect_equal(na_to_level(factor(c("a","b", "b", NA))), factor(c("a","b", "b", "N/A"))) -}) \ No newline at end of file + expect_equal(as.vector(na_to_level(factor(c("a","b", "b", NA)))), + as.vector(factor(c("a","b", "b", "N/A")))) + expect_true(is.factor(na_to_level(factor(c("a","b", "b", NA))))) +})