Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
msberends committed Nov 4, 2019
1 parent 12279f0 commit 1c4cb4d
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 120 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Package: cleaner
Title: Fast and Easy Data Cleaning
Version: 1.2.0
Date: 2019-10-31
Date: 2019-11-04
Authors@R:
person(
given = c("Matthijs", "S."),
family = "Berends",
email = "[email protected]",
role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-7620-1800"))
Description: Data cleaning functions for classes 'logical',
'factor', 'numeric', 'character', 'currency' and 'Date' to make
Description: Data cleaning functions for classes logical,
factor, numeric, character, currency and Date to make
data cleaning fast and easy. Relying on very few dependencies, it
provides smart guessing, but with user options to override
anything if needed.
Expand Down
45 changes: 26 additions & 19 deletions R/freq.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,21 @@
#' @param f a frequency table
#' @param n number of top \emph{n} items to return, use -n for the bottom \emph{n} items. It will include more than \code{n} rows if there are ties.
#' @param property property in header to return this value directly
#' @details Frequency tables (or frequency distributions) are summaries of the distribution of values in a sample. With the `freq` function, you can create univariate frequency tables. Multiple variables will be pasted into one variable, so it forces a univariate distribution. This package also has a vignette available to explain the use of this function further, run \code{browseVignettes("clean")} to read it.
#' @details Frequency tables (or frequency distributions) are summaries of the distribution of values in a sample. With the `freq` function, you can create univariate frequency tables. Multiple variables will be pasted into one variable, so it forces a univariate distribution.
#'
#' Input can be done in many different ways. Base R methods are:
#' \preformatted{
#' freq(df$variable)
#' freq(df[, "variable"])
#' }
#'
#' Tidyverse methods are:
#' \preformatted{
#' df$variable \%>\% freq()
#' df[, "variable"] \%>\% freq()
#' df \%>\% freq("variable")
#' df \%>\% freq(variable)
#' }
#'
#' For numeric values of any class, these additional values will all be calculated with \code{na.rm = TRUE} and shown into the header:
#' \itemize{
Expand Down Expand Up @@ -69,17 +83,17 @@
#' Interested in extending the \code{freq()} function with your own class? Add a method like below to your package, and optionally define some header info by passing a \code{\link{list}} to the \code{.add_header} parameter, like below example for class \code{difftime}. This example assumes that you use the \code{roxygen2} package for package development.
#' \preformatted{
#' #' @exportMethod freq.difftime
#' #' @importFrom clean freq.default
#' #' @importFrom cleaner freq.default
#' #' @export
#' #' @noRd
#' freq.difftime <- function(x, ...) {
#' freq.default(x = x, ...,
#' .add_header = list(units = attributes(x)$units))
#' }
#' }
#' Be sure to call \code{freq.default} in your function and not just \code{freq}. Also, add \code{clean} to the \code{Imports:} field of your \code{DESCRIPTION} file, to make sure that it will be installed with your package, e.g.:
#' Be sure to call \code{freq.default} in your function and not just \code{freq}. Also, add \code{cleaner} to the \code{Imports:} field of your \code{DESCRIPTION} file, to make sure that it will be installed with your package, e.g.:
#' \preformatted{
#' Imports: clean
#' Imports: cleaner
#' }
#' @keywords summary summarise frequency freq
#' @rdname freq
Expand All @@ -88,22 +102,15 @@
#' @export
#' @exportMethod freq
#' @examples
#' \dontrun{
#'
#' # this all gives the same results:
#' freq(df$variable)
#' freq(df[, "variable"])
#' df$variable %>% freq()
#' df[, "variable"] %>% freq()
#' df %>% freq("variable")
#' df %>% freq(variable) # <- tidyverse way
#' }
#' freq(unclean$gender, markdown = FALSE)
#'
#' clean_gender <- clean_factor(unclean$gender,
#' levels = c("^m" = "Male",
#' "^f" = "Female"))
#' freq(unclean$gender)
#' freq(clean_gender)
#' freq(x = clean_factor(unclean$gender,
#' levels = c("^m" = "Male",
#' "^f" = "Female")),
#' markdown = TRUE,
#' title = "Frequencies of a cleaned version for a markdown report!",
#' header = FALSE,
#' quote = TRUE)
freq <- function(x, ...) {
UseMethod("freq")
}
Expand Down
4 changes: 2 additions & 2 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ round2 <- function(x, digits = 0, force_zero = TRUE) {
# https://stackoverflow.com/a/12688836/4575331
val <- (trunc((abs(x) * 10 ^ digits) + 0.5) / 10 ^ digits) * sign(x)
if (digits > 0 & force_zero == TRUE) {
val[val != as.integer(val)] <- paste0(val[val != as.integer(val)],
strrep("0", max(0, digits - nchar(gsub(".*[.](.*)$", "\\1", val[val != as.integer(val)])))))
val[val != as.integer(val) & !is.na(val)] <- paste0(val[val != as.integer(val) & !is.na(val)],
strrep("0", max(0, digits - nchar(gsub(".*[.](.*)$", "\\1", val[val != as.integer(val) & !is.na(val)])))))
}
val
}
Expand Down
10 changes: 0 additions & 10 deletions R/percentage.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@
#'
#' round(0.4455 * 100, 1) # mind the rounding
#' percentage(0.4455) # does not round to 44.5%
#'
#' \dontrun{
#' library(ggplot2)
#' ggplot(data.frame(a = LETTERS[1:6],
#' b = runif(6)),
#' aes(a, b)) +
#' geom_col() +
#' geom_label(aes(label = percentage(b))) +
#' scale_y_continuous(labels = percentage)
#' }
as.percentage <- function(x, ...) {
if (is.percentage(x)) {
return(x)
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/format_datetime.html

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

86 changes: 42 additions & 44 deletions docs/reference/freq.html

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

11 changes: 1 addition & 10 deletions docs/reference/percentage.html

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

Loading

0 comments on commit 1c4cb4d

Please sign in to comment.