mirror of
https://github.com/agdamsbo/daDoctoR.git
synced 2024-11-22 11:50:23 +01:00
29 lines
1.1 KiB
R
29 lines
1.1 KiB
R
#' Easy function for splitting numeric variable in quantiles
|
|
#'
|
|
#' Using base/stats functions cut() and quantile().
|
|
#' @param x Variable to cut.
|
|
#' @param groups Number of groups.
|
|
#' @param y alternative vector to draw quantile cuts from. Limits has to be within x. Default is NULL.
|
|
#' @param na.rm Remove NA's. Default is TRUE.
|
|
#' @param group.names Names of groups to split to. Default is NULL, giving intervals as names.
|
|
#' @param ordered.f Set resulting vector as ordered. Default is FALSE.
|
|
#' @keywords quantile
|
|
#' @export
|
|
#' @examples
|
|
#' aa <- as.numeric(sample(1:1000,2000,replace = TRUE))
|
|
#' summary(quantile_cut(aa,groups=4)) ## Cuts quartiles
|
|
|
|
quantile_cut<-function (x, groups,y=NULL, na.rm = TRUE, group.names = NULL, ordered.f = FALSE)
|
|
{
|
|
if (!is.null(y)){
|
|
q<-quantile(y, probs = seq(0, 1, 1/groups), na.rm = na.rm, names = TRUE, type = 7)
|
|
|
|
}
|
|
if (is.null(y)){
|
|
q<-quantile(x, probs = seq(0, 1, 1/groups), na.rm = na.rm, names = TRUE, type = 7)
|
|
}
|
|
d<-cut(x, q, include.lowest = TRUE, labels = group.names,
|
|
ordered_result = ordered.f)
|
|
return(list(d,q))
|
|
}
|