Skip to content

Commit

Permalink
rewrite categorize() close #7 🎨
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Cazelles committed Jun 21, 2019
1 parent 7e39b84 commit 00fe13e
Show file tree
Hide file tree
Showing 20 changed files with 231 additions and 256 deletions.
5 changes: 4 additions & 1 deletion News.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

- README simplified


## Changes in functions :gear:

- remove (see #8)
- categorize has been rewritten (see #7)
- remove `stLength()` (see #9)
- remove `logit()` (see #8)
- In `dfTemplate()` the values of argument `col_classes` are now checked, a test has been added.
- In `dfTemplate()` `fill` is now a vector whose values are replicated so its length equals the number of columns.
- In `dfTemplate()`, `col_classes` values are replicated so its length equals the number of columns.
Expand Down
6 changes: 3 additions & 3 deletions R/adjustString.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
#' to be created.
#' @param extra character vector, or a vector to be coerced to a character
#' vector that will be (partially) added to produced a string of
#' @param before logical. If TRUE, then the extra characters are added before
#' @param before logical. If `TRUE`, then the extra characters are added before
#' \code{x}
#'
#' @details
#' This function was originally created to help getting a fixed number of digits
#' when naming files. The current version is more general but it remains extremely
#' usefull to name files.
#' when naming files. The current version is more general but it remains
#' extremely useful to name files.
#'
#' @return
#' A character vector of the concatenated characters.
Expand Down
27 changes: 10 additions & 17 deletions R/categorize.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
#' Assign categories to a vector of values.
#'
#' Assigns a category to each element of a vector for a given set of threshold
#' values. Note that categories are always sorted. By defaults trheshold values
#' are regarded as upper boiundaries.
#' values..
#'
#' @author
#' Kevin Cazelles
#' @param x A numeric, complex, character or logical vector.
#' @param categ A set of threshold that are used to assign categories.
#' @param lower A logical, if `TRUE` then `categ` are considered elements equal to a given threshold values are included in the lower category, default is FALSE.
#' @param categ A set of threshold values used to assign categories.
#' @param lower A logical, if `TRUE` threshold values (i.e. values within
#' `categ`) belongs the the lower category rather than the upper (default
#' behaviour).
#' @return
#' A vector of categories assinged.
#' A vector of categories assigned.
#' @export
#' @examples
#' categorize(stats::runif(40), categ=c(0.5,0.75))
#' categorize(LETTERS[1:5], categ='C')
#' categorize(LETTERS[1:5], categ='C', lower=TRUE)
#' categorize(LETTERS[1:5], categ='C', lower=TRUE)
#' categorize(LETTERS[floor(5*stats::runif(20))+1], categ=LETTERS[1:5], lower=TRUE)


categorize <- function(x, categ, lower = FALSE) {
categ <- sort(unique(categ))
out <- rep(1, length(x))
##
categ <- unique(categ)
if (lower) {
id <- which(x > categ[1L])
out[id] <- out[id] + sapply(x[id], function(z) max(which(z > categ)))
} else {
id <- which(x >= categ[1L])
out[id] <- out[id] + sapply(x[id], function(z) max(which(z >= categ)))
}
##
out
unlist(lapply(x, function(y) sum(y > categ) + 1))
} else unlist(lapply(x, function(y) sum(y >= categ) + 1))
}
2 changes: 1 addition & 1 deletion R/packagesUsed.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#' Get a data frame of package and their installed version.
#'
#' @description
#' Return a table of packages and their version.,
#' Returns a table of packages and their version.
#'
#' @author
#' Kevin Cazelles
Expand Down
16 changes: 7 additions & 9 deletions R/stApply.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,24 @@
#'
#' @return
#' A character vector.
#'
#' @importFrom magrittr %>% %<>%
#' @export
#' @examples
#' stApply('cool', pos = 1:2, FUN = toupper)
#' stApply(c('cool', 'pro'), pattern = 'o', FUN = toupper)


stApply <- function(x, FUN, pos = NULL, pattern = NULL) {
if (!is.character(x))

if (!is.character(x))
x <- as.character(x)

if (!is.null(pos)) {
tmp <- strsplit(x, split = "")
tmp_fun <- function(x) {
x[pos] %<>% FUN
x[pos] <- FUN(x[pos])
paste(x, collapse = "")
}
out <- lapply(tmp, tmp_fun) %>% unlist
out <- unlist(lapply(tmp, tmp_fun))
} else {
if (is.null(pattern)) {
warning("neither pos nor pattern is defined", call. = FALSE)
Expand All @@ -50,7 +48,7 @@ stApply <- function(x, FUN, pos = NULL, pattern = NULL) {
out <- apply(cbind(tmp_mth, tmp_inv), 1, FUN = reassemble, f = FUN)
}
}

out
}

Expand All @@ -61,7 +59,7 @@ reassemble <- function(x, f) {
sz <- length(char1) + length(char2)
out <- rep("", sz)
out[seq(1, sz, 2)] <- char2
if (sz > 1)
if (sz > 1)
out[seq(2, sz - 1, 2)] <- f(x[1L][[1L]])
paste(out, collapse = "")
}
16 changes: 8 additions & 8 deletions R/wordCount.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Count the words in your character strings.
#' Count words
#'
#' Function that counts the number of word in a character string.
#'
Expand All @@ -10,19 +10,19 @@
#'
#' @return A vector of the number of word in each element of \code{sting}.
#' @importFrom magrittr %>%
#' @importFrom magrittr %<>%
#' @export
#' @examples
#' strex <- 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.'
#' wordCount(strex)
#' wordCount(c(strex,'A second chacter string.'))
#' wordCount(c(strex,'A second character string.'))


wordCount <- function(string, exclude = "[[:punct:]]") {
##
string %<>% as.character
string %<>% gsub(pattern = exclude, replacement = " ") %>% gsub(pattern = " +",
replacement = " ")
##
string %>% strsplit(split = " ") %>% lapply(length) %>% unlist
string %>% as.character %>%
gsub(pattern = exclude, replacement = " ") %>%
gsub(pattern = " +", replacement = " ") %>%
strsplit(split = " ") %>%
lapply(length) %>%
unlist
}
8 changes: 5 additions & 3 deletions docs/News.html

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

Loading

0 comments on commit 00fe13e

Please sign in to comment.