diff --git a/NAMESPACE b/NAMESPACE index d75f824..c1a7cea 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1 +1,2 @@ exportPattern("^[[:alpha:]]+") +importFrom("stats", "quantile") diff --git a/R/meanimpute.R b/R/meanimpute.R index cc7cf5e..9023102 100644 --- a/R/meanimpute.R +++ b/R/meanimpute.R @@ -1,4 +1,6 @@ #' Meanimputation +#' Calculates mean of a given vector, ignores NA values. +#' @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..c9045b9 --- /dev/null +++ b/R/transform_log.R @@ -0,0 +1,18 @@ +#' transform_log +#' Transform numerical values into their log values +#' @param x A vector +#' @return logarithm of x +#' @examples +#' transform_log(c(NA,0,-1,exp(2))) +#'@export +transform_log<-function(x){ + if(!is.numeric(x))stop("function is expecting only numeric values") + x_nan<-is.na(x) + x[x_nan]<-1 + ifelse(x<0,"OK", warning("input vector contains negative values, turned into NA")) + y<-log(x[x>=0]) + x[x>=0]<-y + x[x<0]<-NA + x[x_nan]<-NA + x +} \ No newline at end of file diff --git a/R/windsorize.R b/R/windsorize.R index b4e15e6..85044f8 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -1,10 +1,23 @@ #' Windsorize #' -#' Do some windsorization. +#' +#' Transform all outliner data to +#' (1-p)/2 percentile value for lower outliers and +#' (1+p)/2 for higher outliers. +#' +#' @param x A vector. +#' @param p A quantile. +#' @return inuput vector x with trimmed outliers by (1-p) percentile. +#' @examples +#' windsorize(rnorm(100,0,1)) #' @export windsorize <- function(x, p = .90) { - q <- quantile(x, p) - x[x >= q] <- q + if(is.null(x))stop("vector is empty") + y<-x[!is.na(x)] + if(length(y)==0)stop("vector contains only NAs") + q_max <- quantile(y, (1+p)/2) + q_min<- quantile(y,(1-p)/2) + x[x >= q_max] <- q_max + x[x<= q_min]<-q_min x -} - +} \ No newline at end of file diff --git a/man/meanimpute.Rd b/man/meanimpute.Rd index 8139e8f..77f2fbc 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..c42f726 --- /dev/null +++ b/man/transform_log.Rd @@ -0,0 +1,22 @@ +% 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 +Transform numerical values into their log values} +\usage{ +transform_log(x) +} +\arguments{ +\item{x}{A vector} +} +\value{ +logarithm of x +} +\description{ +transform_log +Transform numerical values into their log values +} +\examples{ +transform_log(c(NA,0,-1,exp(2))) +} diff --git a/man/windsorize.Rd b/man/windsorize.Rd index 832c3cb..dcabd36 100644 --- a/man/windsorize.Rd +++ b/man/windsorize.Rd @@ -6,6 +6,19 @@ \usage{ windsorize(x, p = 0.9) } +\arguments{ +\item{x}{A vector.} + +\item{p}{A quantile.} +} +\value{ +inuput vector x with trimmed outliers by (1-p) percentile. +} \description{ -Do some windsorization. +Transform all outliner data to +(1-p)/2 percentile value for lower outliers and +(1+p)/2 for higher outliers. +} +\examples{ +windsorize(rnorm(100,0,1)) }