forked from ProjectMOSAIC/mosaic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactorize.R
55 lines (49 loc) · 1.46 KB
/
factorize.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#' Conditionally convert vectors to factors
#'
#' A generic function and several instances for creating factors from
#' other sorts of data. The primary use case is for vectors that contain
#' few unique values and might be better considered as factors. When
#' applied to a data frame, this is applied to each variable in the data frame.
#'
#' @param x an object
#' @param max.levels an integer. Only convert if the number of unique values is no
#' more than `max.levels`.
#' @param ... additional arguments (currently ignored)
#'
#' @export
#' @examples
#' data(KidsFeet, package="mosaicData")
#' str(KidsFeet)
#' factorize(KidsFeet$birthyear)
#' str(factorize(KidsFeet))
#' # alternative spelling
#' str(factorise(KidsFeet))
factorize <- function(x, ...) {
UseMethod("factorize")
}
#' @rdname factorize
#' @export
factorize.default <- function(x, ...) {
x
}
#' @rdname factorize
#' @export
factorize.numeric <- function(x, max.levels = 5L, ...){
if (length(unique(x)) <= max.levels) return ( factor(x, levels=sort(unique(x))) )
x
}
#' @rdname factorize
#' @export
factorize.character <- function(x, max.levels = 5L, ...){
if (length(unique(x)) <= max.levels) return ( factor(x, levels=sort(unique(x))) )
x
}
#' @rdname factorize
#' @export
factorize.data.frame <- function(x, max.levels=5L, ...) {
as.data.frame( lapply(x, factorize, max.levels=max.levels) )
}
# an alias with alternative spelling
#' @rdname factorize
#' @export
factorise <- factorize