Skip to content
Open
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
exportPattern("^[[:alpha:]]+")
importFrom("stats", "quantile")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why haven't you used the package roxygen2 to generate the NAMESPACE file?

2 changes: 2 additions & 0 deletions R/meanimpute.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#' Meanimputation
#' @param x A vector.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Longer description missing.

#'
#' @export
meanimpute <- function(x) {
x[is.na(x)] <- mean(x, na.rm = TRUE)
Expand Down
16 changes: 16 additions & 0 deletions R/transform_log.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#' Transform_log
#'
#' Log transformation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Description could be longer here.

#'
#' @param x A vector.
#'
#' @examples
#' transform_log(exp(rnorm(2)))
#'
#' @export

transform_log <- function(x) {
if (any(x<0)) {stop("input can't be negative")}
x <- log(x)
x
}
20 changes: 16 additions & 4 deletions R/windsorize.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#' Windsorize
#'
#' Do some windsorization.
#'
#' Winsorizing or winsorization is the transformation of statistics by limiting
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice description! Just missing how input parameter p influences the result

#' extreme values in the statistical data to reduce the effect of possibly
#' spurious outliers.
#'
#' @param x A vector.
#' @param p A quantile.
#'
#' @examples
#' windsorize(rnorm(5))
#'
#' @export
windsorize <- function(x, p = .90) {
q <- quantile(x, p)
x[x >= q] <- q
if(all(is.na(x))) {stop("argument should not be a vector containing only NA-s or NULL-s")}
q_up <- quantile(x, 0.5 + p / 2 )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to consider the following situations:

  • What happens if x contains some NA values?
  • What happens if x is an empty/NULL vector?

q_down <- quantile(x, 0.5 - p / 2 )
x[x >= q_up] <- q_up
x[x <= q_down] <- q_down
x
}

3 changes: 3 additions & 0 deletions man/meanimpute.Rd

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

18 changes: 18 additions & 0 deletions man/transform_log.Rd

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

13 changes: 12 additions & 1 deletion man/windsorize.Rd

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

4 changes: 4 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(datacleaner)

test_check("datacleaner")
5 changes: 5 additions & 0 deletions tests/testthat/test_transform_log.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
context("No negative values")
library(datacleaner)
test_that("There are no negative values in input", {
expect_error(transform_log(c(1,2,-1)), "input can't be negative")
})
6 changes: 6 additions & 0 deletions tests/testthat/test_windsorize.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
context("NA and null values")
library(datacleaner)
test_that("NA and null values produce error message", {
expect_error(windsorize(NA), "argument should not be a vector containing only NA-s or NULL-s")
expect_error(windsorize(NULL), "argument should not be a vector containing only NA-s or NULL-s")
})