Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Title: Data Cleaning - what else?
Version: 0.1.0
Author: Quantargo Instructor
Maintainer: Quantargo Instructor <courses@quantargo.com>
Imports: stats
Description: Some lengthy description which somebody should write
Sometime.
License: GPL-3
Expand Down
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.

Have you used roxygen2 to generate the file here?

9 changes: 9 additions & 0 deletions R/meanimpute.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#' Meanimputation
#'
#' Replaces NA values with the mean of \code{x}
#'
#' @param x A numeric vector containing real numbers
#' @return Imputed vector \code{x} where NAs are replaced with the mean of \code{x}
#' @examples
#' meanimpute(x = c(1, NA, 3, 4, 5, 6, 7, 8, NA, 10))
#' meanimpute(c(1, NA, 3, 4, 5, 6, 7, 8, NA, 10))
#' @export

meanimpute <- function(x) {
x[is.na(x)] <- mean(x, na.rm = TRUE)
x
Expand Down
26 changes: 26 additions & 0 deletions R/transform_log.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#' Transform_log
#'
#' Transforms values to natural logarithm.
#'
#' @param x A numeric vector containing positive real numbers
#' @return Natural logarithm of the values in \code{x}.
#' @examples
#' transform_log(x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
#' transform_log(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
#' @export

transform_log <- function(x){

if( sum(is.na(x)) ){
Copy link
Member

Choose a reason for hiding this comment

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

Why do NA values lead to an error here?
Sidenote: Although correct it would be more explicit to use any(is.na(x))

stop("Input x contains value NA")
}
else if( length(x) == 0 ){
stop("Input x is NULL")
}
else if( sum(x <= 0) ){
Copy link
Member

Choose a reason for hiding this comment

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

Again, more explicit to use any(x <= 0). What could be the workaround for negative values here?

stop("Input x contains non-positive values")
}

log(x)

}
23 changes: 21 additions & 2 deletions R/windsorize.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
#' Windsorize
#'
#' Do some windsorization.
Copy link
Member

Choose a reason for hiding this comment

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

Description should be more explicit.

#'
#' @param x A numeric vector containing real numbers
#' @param p A number between 0 and 1, representing the mass of probability. Values are winsorized outside \code{p}. Default value is 0.9.
#' @return Windsorized vector of \code{x}.
#' @examples
#' windsorize(x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
#' windsorize(x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), p = 0.8)
#' windsorize(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 0.8)
#' @export

windsorize <- function(x, p = .90) {
q <- quantile(x, p)
x[x >= q] <- q

if( sum(is.na(x))){
Copy link
Member

Choose a reason for hiding this comment

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

Again, any() is more explicit.

stop("Input x contains value NA")
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to throw an error here? What could be the workaround for NA values?

}
else if( length(x) == 0 ){
stop("Input x is NULL")
}

q_lower <- quantile(x, (1-p)/2)
q_upper <- quantile(x, p + (1-p)/2)
x[x <= q_lower] <- q_lower
x[x >= q_upper] <- q_upper
x
}

12 changes: 11 additions & 1 deletion man/meanimpute.Rd

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

21 changes: 21 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: 13 additions & 0 deletions 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")
19 changes: 19 additions & 0 deletions tests/testthat/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
library(datacleaner)

test_that("windsorize the input vector", {
expect_error(windsorize(c(NULL)), "Input x is NULL")
expect_error(windsorize(c(1, 2, NA)), "Input x contains value NA")
expect_equal(windsorize(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 0.8), c(1.9, 2, 3, 4, 5, 6, 7, 8, 9, 9.1))
})

test_that("Takes natural logarithm", {
expect_error(transform_log(c(NULL)), "Input x is NULL")
expect_error(transform_log(c(1, 2, NA)), "Input x contains value NA")
expect_error(transform_log(c(-1, 2, 3)), "Input x contains non-positive values")
expect_equal(transform_log(c(1, exp(1))), c(0, 1))
})

test_that("Takes natural logarithm", {
expect_equal(meanimpute(c(1, NA, 3)), c(1, 2, 3))
expect_equal(meanimpute(c(1, 2 , 3)), c(1, 2, 3))
})