diff --git a/DESCRIPTION b/DESCRIPTION index 492ae33..cad592e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,7 +10,7 @@ License: GPL-3 Encoding: UTF-8 LazyData: true Suggests: - testthat, + testthat (>= 2.1.0), covr, rmarkdown RoxygenNote: 6.1.1 diff --git a/NAMESPACE b/NAMESPACE index d75f824..934d9b3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1 +1,5 @@ -exportPattern("^[[:alpha:]]+") +# Generated by roxygen2: do not edit by hand + +export(meanimpute) +export(transform_log) +export(windsorize) diff --git a/R/meanimpute.R b/R/meanimpute.R index cc7cf5e..d09ca51 100644 --- a/R/meanimpute.R +++ b/R/meanimpute.R @@ -1,4 +1,9 @@ #' Meanimputation +#' +#' @param x A numeric vector containing the data +#' @return the input vector with its NA values replaced by the mean +#' @examples +#' log(1:5) #' @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..db716d8 --- /dev/null +++ b/R/transform_log.R @@ -0,0 +1,17 @@ +#' Log transforms data. +#' +#' @param x A numeric vector containing the data +#' @return A numeric vector whose entries are the logarithms of the input. +#' @examples +#' log(1:5) +#' @export +transform_log<-function(x){ + if(length(x)==0){ + stop("x must have positive length") + }else if(sum(x<=0)>0){ + stop("x must not contain non-positive values") + }else if(sum(is.na(x))>0){ + stop("x must not contain any NA") + } + log(x) +} \ No newline at end of file diff --git a/R/windsorize.R b/R/windsorize.R index b4e15e6..dba1fda 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -1,10 +1,20 @@ -#' Windsorize +#' Windsorizes data. #' -#' Do some windsorization. +#' @param x A numeric vector containing the data. +#' @param p A numeric value denoting the a probability +#' @return A numeric vector containing the windsorized data, i.e. extreme values are replaced by the quantiles derived from \code{p}. +#' @examples +#' windsorize(c(92 , 19 , 101 , 58 , 1053 , 91 , 26 , 78 , 10 , 13 , -40 , 101 , 86 , 85 , 15 , 89 , 89 , 28 , -5 , 41)) #' @export windsorize <- function(x, p = .90) { - q <- quantile(x, p) - x[x >= q] <- q + if(length(x)==0){ + stop("x must be of positive length") + }else if(sum(is.na(x))>0){ + stop("x must not contain any NA") + } + q <- quantile(x, 0.5 * ( 1 + p * c(-1,1) ) ) + x[x >= q[2] ] <- q[2] + x[x <= q[1] ] <- q[1] x } diff --git a/man/meanimpute.Rd b/man/meanimpute.Rd index 8139e8f..5984f37 100644 --- a/man/meanimpute.Rd +++ b/man/meanimpute.Rd @@ -6,6 +6,15 @@ \usage{ meanimpute(x) } +\arguments{ +\item{x}{A numeric vector containing the data} +} +\value{ +the input vector with its NA values replaced by the mean +} \description{ Meanimputation } +\examples{ +log(1:5) +} diff --git a/man/transform_log.Rd b/man/transform_log.Rd new file mode 100644 index 0000000..b7d9dce --- /dev/null +++ b/man/transform_log.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/transform_log.R +\name{transform_log} +\alias{transform_log} +\title{Log transforms data.} +\usage{ +transform_log(x) +} +\arguments{ +\item{x}{A numeric vector containing the data} +} +\value{ +A numeric vector whose entries are the logarithms of the input. +} +\description{ +Log transforms data. +} +\examples{ +log(1:5) +} diff --git a/man/windsorize.Rd b/man/windsorize.Rd index 832c3cb..ed90eb5 100644 --- a/man/windsorize.Rd +++ b/man/windsorize.Rd @@ -2,10 +2,21 @@ % Please edit documentation in R/windsorize.R \name{windsorize} \alias{windsorize} -\title{Windsorize} +\title{Windsorizes data.} \usage{ windsorize(x, p = 0.9) } +\arguments{ +\item{x}{A numeric vector containing the data.} + +\item{p}{A numeric value denoting the a probability} +} +\value{ +A numeric vector containing the windsorized data, i.e. extreme values are replaced by the quantiles derived from \code{p}. +} \description{ -Do some windsorization. +Windsorizes data. +} +\examples{ +windsorize(c(92 , 19 , 101 , 58 , 1053 , 91 , 26 , 78 , 10 , 13 , -40 , 101 , 86 , 85 , 15 , 89 , 89 , 28 , -5 , 41)) } 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-windsorize.R b/tests/testthat/test-windsorize.R new file mode 100644 index 0000000..42d45df --- /dev/null +++ b/tests/testthat/test-windsorize.R @@ -0,0 +1,3 @@ +test_that("windsorize works", { + expect_equal(windsorize(3:10), c(3.35,4.00,5.00,6.00,7.00,8.00,9.00,9.65)) +})