diff --git a/NAMESPACE b/NAMESPACE index d75f824..1015c5a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1 +1,2 @@ exportPattern("^[[:alpha:]]+") +importFrom("stats", "quantile") \ No newline at end of file diff --git a/R/meanimpute.R b/R/meanimpute.R index cc7cf5e..c3fe624 100644 --- a/R/meanimpute.R +++ b/R/meanimpute.R @@ -1,4 +1,6 @@ #' Meanimputation +#' @param x A vector. +#' #' @export meanimpute <- function(x) { x[is.na(x)] <- mean(x, na.rm = TRUE) diff --git a/R/transform_log.R b/R/transform_log.R new file mode 100644 index 0000000..157bd49 --- /dev/null +++ b/R/transform_log.R @@ -0,0 +1,16 @@ +#' Transform_log +#' +#' Log transformation. +#' +#' @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 +} \ No newline at end of file diff --git a/R/windsorize.R b/R/windsorize.R index b4e15e6..9438ac7 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -1,10 +1,22 @@ #' Windsorize -#' -#' Do some windsorization. +#' +#' Winsorizing or winsorization is the transformation of statistics by limiting +#' 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 ) + q_down <- quantile(x, 0.5 - p / 2 ) + x[x >= q_up] <- q_up + x[x <= q_down] <- q_down x } diff --git a/man/meanimpute.Rd b/man/meanimpute.Rd index 8139e8f..7773d13 100644 --- a/man/meanimpute.Rd +++ b/man/meanimpute.Rd @@ -6,6 +6,9 @@ \usage{ meanimpute(x) } +\arguments{ +\item{x}{A vector.} +} \description{ Meanimputation } diff --git a/man/transform_log.Rd b/man/transform_log.Rd new file mode 100644 index 0000000..e77f2c3 --- /dev/null +++ b/man/transform_log.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/transform_log.R +\name{transform_log} +\alias{transform_log} +\title{Transform_log} +\usage{ +transform_log(x) +} +\arguments{ +\item{x}{A vector.} +} +\description{ +Log transformation. +} +\examples{ +transform_log(exp(rnorm(2))) + +} diff --git a/man/windsorize.Rd b/man/windsorize.Rd index 832c3cb..249fecc 100644 --- a/man/windsorize.Rd +++ b/man/windsorize.Rd @@ -6,6 +6,17 @@ \usage{ windsorize(x, p = 0.9) } +\arguments{ +\item{x}{A vector.} + +\item{p}{A quantile.} +} \description{ -Do some windsorization. +Winsorizing or winsorization is the transformation of statistics by limiting +extreme values in the statistical data to reduce the effect of possibly +spurious outliers. +} +\examples{ +windsorize(rnorm(5)) + } diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..8adbfff --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(datacleaner) + +test_check("datacleaner") diff --git a/tests/testthat/test_transform_log.R b/tests/testthat/test_transform_log.R new file mode 100644 index 0000000..9b96c99 --- /dev/null +++ b/tests/testthat/test_transform_log.R @@ -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") +}) \ No newline at end of file diff --git a/tests/testthat/test_windsorize.R b/tests/testthat/test_windsorize.R new file mode 100644 index 0000000..4e19ba5 --- /dev/null +++ b/tests/testthat/test_windsorize.R @@ -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") +}) \ No newline at end of file