diff --git a/DESCRIPTION b/DESCRIPTION index d1ca2b1..c2eed51 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: CTUHelpR Title: Small helper functions used in the CTU Basel -Version: 0.0.3 +Version: 0.0.4 Author: c(person(given = "CTUBasel", role = c("cph")), @@ -29,7 +29,8 @@ RoxygenNote: 7.1.0 Depends: R (>= 3.5) Imports: - readr + readr, + assertthat Suggests: knitr, lintr, diff --git a/NAMESPACE b/NAMESPACE index 8212ed8..413316d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,8 @@ # Generated by roxygen2: do not edit by hand +export(na_to_level) export(Median) export(Sum) export(print_file_content) +importFrom(assertthat,assert_that) importFrom(readr,read_file) diff --git a/NEWS.md b/NEWS.md index eb00ae0..77f7a0b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ + +# CTUHelpR 0.0.4 +* the `na_to_level()` function has been added to the package + # CTUHelpR 0.0.3 * the `Median()` function has been added to the package diff --git a/R/na_to_level.R b/R/na_to_level.R new file mode 100644 index 0000000..f596ab4 --- /dev/null +++ b/R/na_to_level.R @@ -0,0 +1,19 @@ +#' 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 +#' @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") { + 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/README.md b/README.md index fb38cf0..0e89b4e 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,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.3-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.4-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 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..5346943 --- /dev/null +++ b/tests/testthat/test-na_to_level.R @@ -0,0 +1,12 @@ +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(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))))) +})