From c84ad5c651ecc7838241fc324c210920edb9921b Mon Sep 17 00:00:00 2001 From: Vilma Shehu <49863006+VilmaShehu@users.noreply.github.com> Date: Sat, 4 May 2019 18:49:54 +0200 Subject: [PATCH 1/9] Add files via upload --- meanimpute.R | 6 ++++++ transformlog.R | 8 ++++++++ windsorize.R | 13 +++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 meanimpute.R create mode 100644 transformlog.R create mode 100644 windsorize.R diff --git a/meanimpute.R b/meanimpute.R new file mode 100644 index 0000000..3167cb7 --- /dev/null +++ b/meanimpute.R @@ -0,0 +1,6 @@ +#' Meanimputation +#' @export +meanimpute <- function(x) { + x[is.na(x)] <- mean(x, na.rm = TRUE) + x +} diff --git a/transformlog.R b/transformlog.R new file mode 100644 index 0000000..0ca7b8e --- /dev/null +++ b/transformlog.R @@ -0,0 +1,8 @@ +transform_log<- function(x){ + if (x<0) stop("Negative input not allowed") + if (x==0) stop("Input 0 not allowed") + if (!is.numeric(x)) stop("Not numeric input not allowed") + if (is.null(x)) stop("NULL input not allowed") + + log(x) +} diff --git a/windsorize.R b/windsorize.R new file mode 100644 index 0000000..21f453e --- /dev/null +++ b/windsorize.R @@ -0,0 +1,13 @@ +#' Windsorize +#' +#' Do some windsorization. +#' @export +windsorize <- function(x, p = .90) { + if (length(x) == 0) stop("Not allowed the use of an empty vector!") + if (all(is.na(x))) stop("Not allowed the use of a vector containing only NA!") + + q <- quantile(x, probs=c(1-p,p)) + x[x >= q] <- q + x +} + From 6d4c3c0aae8546f61ac0cf698cf037874d0e2c62 Mon Sep 17 00:00:00 2001 From: Vilma Shehu <49863006+VilmaShehu@users.noreply.github.com> Date: Sat, 4 May 2019 20:48:42 +0200 Subject: [PATCH 2/9] Delete meanimpute.R --- meanimpute.R | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 meanimpute.R diff --git a/meanimpute.R b/meanimpute.R deleted file mode 100644 index 3167cb7..0000000 --- a/meanimpute.R +++ /dev/null @@ -1,6 +0,0 @@ -#' Meanimputation -#' @export -meanimpute <- function(x) { - x[is.na(x)] <- mean(x, na.rm = TRUE) - x -} From 9c92805e1d1d13f4707895c620c3c2e1953c89fa Mon Sep 17 00:00:00 2001 From: Vilma Shehu <49863006+VilmaShehu@users.noreply.github.com> Date: Sat, 4 May 2019 20:49:38 +0200 Subject: [PATCH 3/9] Delete windsorize.R --- windsorize.R | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 windsorize.R diff --git a/windsorize.R b/windsorize.R deleted file mode 100644 index 21f453e..0000000 --- a/windsorize.R +++ /dev/null @@ -1,13 +0,0 @@ -#' Windsorize -#' -#' Do some windsorization. -#' @export -windsorize <- function(x, p = .90) { - if (length(x) == 0) stop("Not allowed the use of an empty vector!") - if (all(is.na(x))) stop("Not allowed the use of a vector containing only NA!") - - q <- quantile(x, probs=c(1-p,p)) - x[x >= q] <- q - x -} - From ef35c5c26493f53730870ddfa7180f465fe45f6b Mon Sep 17 00:00:00 2001 From: Vilma Shehu <49863006+VilmaShehu@users.noreply.github.com> Date: Sat, 4 May 2019 20:49:52 +0200 Subject: [PATCH 4/9] Delete transformlog.R --- transformlog.R | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 transformlog.R diff --git a/transformlog.R b/transformlog.R deleted file mode 100644 index 0ca7b8e..0000000 --- a/transformlog.R +++ /dev/null @@ -1,8 +0,0 @@ -transform_log<- function(x){ - if (x<0) stop("Negative input not allowed") - if (x==0) stop("Input 0 not allowed") - if (!is.numeric(x)) stop("Not numeric input not allowed") - if (is.null(x)) stop("NULL input not allowed") - - log(x) -} From e3f08e4ff975d4a8a6d6ebba004de477c9c758c6 Mon Sep 17 00:00:00 2001 From: Vilma Date: Sat, 4 May 2019 21:13:10 +0200 Subject: [PATCH 5/9] transformlog() function was added. This fixes #17 --- R/transformlog.R | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 R/transformlog.R diff --git a/R/transformlog.R b/R/transformlog.R new file mode 100644 index 0000000..f0018ac --- /dev/null +++ b/R/transformlog.R @@ -0,0 +1,8 @@ +transform_log<- function(x){ + if (x<0) stop("Negative input not allowed") + if (x==0) stop("Input 0 not allowed") + if (!is.numeric(x)) stop("Not numeric input not allowed") + if (is.null(x)) stop("NULL input not allowed") + + log(x) +} From db76d37dd65534cd490dfb2f777448e9dacffef7 Mon Sep 17 00:00:00 2001 From: Vilma Date: Sat, 4 May 2019 21:14:49 +0200 Subject: [PATCH 6/9] windsorize() was corrected to do the necessary cheks on its allowed arguments and to consider the smallest value of the vector. This fixes #15 & #16 --- R/windsorize.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/windsorize.R b/R/windsorize.R index b4e15e6..100c5aa 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -3,7 +3,10 @@ #' Do some windsorization. #' @export windsorize <- function(x, p = .90) { - q <- quantile(x, p) + if (length(x) == 0) stop("Not allowed the use of an empty vector!") + if (all(is.na(x))) stop("Not allowed the use of a vector containing only NA!") + + q <- quantile(x, probs=c(1-p,p)) x[x >= q] <- q x } From 93444639c63c50ff8836a3273109b635e6525597 Mon Sep 17 00:00:00 2001 From: Vilma Date: Sat, 4 May 2019 21:40:39 +0200 Subject: [PATCH 7/9] Param added --- R/meanimpute.R | 5 ++--- R/windsorize.R | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/R/meanimpute.R b/R/meanimpute.R index cc7cf5e..2fc9634 100644 --- a/R/meanimpute.R +++ b/R/meanimpute.R @@ -1,6 +1,5 @@ #' Meanimputation +#' @param x A vector #' @export meanimpute <- function(x) { - x[is.na(x)] <- mean(x, na.rm = TRUE) - x -} + x[is.na(x)] <- mean(x, na.rm = TRUE) \ No newline at end of file diff --git a/R/windsorize.R b/R/windsorize.R index 100c5aa..e052081 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -3,8 +3,8 @@ #' Do some windsorization. #' @export windsorize <- function(x, p = .90) { - if (length(x) == 0) stop("Not allowed the use of an empty vector!") - if (all(is.na(x))) stop("Not allowed the use of a vector containing only NA!") + if (length(x) == 0){ stop("Not allowed the use of an empty vector!")} + if (all(is.na(x))) { stop("Not allowed the use of a vector containing only NA!")} q <- quantile(x, probs=c(1-p,p)) x[x >= q] <- q From bd19d6e67920753e3c47011b6f2c676305b8cee9 Mon Sep 17 00:00:00 2001 From: Vilma Shehu <49863006+VilmaShehu@users.noreply.github.com> Date: Sat, 4 May 2019 21:59:32 +0200 Subject: [PATCH 8/9] Update NAMESPACE --- NAMESPACE | 1 + 1 file changed, 1 insertion(+) diff --git a/NAMESPACE b/NAMESPACE index d75f824..c1a7cea 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1 +1,2 @@ exportPattern("^[[:alpha:]]+") +importFrom("stats", "quantile") From baca68a861e894773f424889eaf9f38147856e70 Mon Sep 17 00:00:00 2001 From: Vilma Shehu <49863006+VilmaShehu@users.noreply.github.com> Date: Sat, 4 May 2019 22:14:08 +0200 Subject: [PATCH 9/9] Update windsorize.R --- R/windsorize.R | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/R/windsorize.R b/R/windsorize.R index e052081..b961541 100644 --- a/R/windsorize.R +++ b/R/windsorize.R @@ -1,13 +1,20 @@ #' Windsorize #' #' Do some windsorization. +#' @param x A numerical vector. +#' @param p Quantile value for outliers removal +#' @examples +#' windsorize(c(5,10,15,50)) #' @export windsorize <- function(x, p = .90) { - if (length(x) == 0){ stop("Not allowed the use of an empty vector!")} - if (all(is.na(x))) { stop("Not allowed the use of a vector containing only NA!")} - - q <- quantile(x, probs=c(1-p,p)) - x[x >= q] <- q + if (length(x) == 0) + { stop("Not allowed the use of an empty vector!")} + if (all(is.na(x))) + { stop("Not allowed the use of a vector containing only NA!")} + + q <- quantile(x, probs=c(1-p,p) , na.rm = TRUE) + x[x >= q[2] ] <- q[2] + x[x <= q[1] ] <- q[1] x }