-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_category.R
More file actions
36 lines (34 loc) · 871 Bytes
/
is_category.R
File metadata and controls
36 lines (34 loc) · 871 Bytes
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
#' @title
#' Test if a column in a dataset is a categorical variable.
#'
#' @description
#' `r lifecycle::badge("experimental")`
#' Test if a column in a dataset is a categorical variable. This function mainly
#' helps validate input within other functions of the package.
#'
#' @param x object to be coerced.
#'
#' @return
#' A logical.
#'
#' @examples
#' {
#'
#' library(dplyr)
#' iris %>% summarise(across(everything(), is_category))
#' is_category(iris[['Species']])
#'
#'}
#'
#' @import dplyr haven
#' @importFrom rlang .data
#'
#' @noRd
is_category <- function(column, threshold = NULL) {
unique_column <- unique(column)
if(is.factor(unique_column)) return(TRUE)
if(is.labelled(unique_column)) return(TRUE)
if(all(is.na(unique_column))) return(FALSE)
if(is.null(threshold)) return(FALSE)
return(length(unique_column) <= threshold)
}