Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand Down Expand Up @@ -29,7 +29,8 @@ RoxygenNote: 7.1.0
Depends:
R (>= 3.5)
Imports:
readr
readr,
assertthat
Suggests:
knitr,
lintr,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
19 changes: 19 additions & 0 deletions R/na_to_level.R
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->


# 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

Expand Down
22 changes: 22 additions & 0 deletions man/na_to_level.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat/test-na_to_level.R
Original file line number Diff line number Diff line change
@@ -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)))))
})