-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kevin Cazelles
committed
Jun 21, 2019
1 parent
7e39b84
commit 00fe13e
Showing
20 changed files
with
231 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.