now interprets empty variables with empty levels attribute as logicals to avoid returning factors with empty levels

This commit is contained in:
Andreas Gammelgaard Damsbo 2025-02-25 10:36:37 +01:00
parent 3eea26223b
commit 7dfbb9b549
No known key found for this signature in database
3 changed files with 31 additions and 5 deletions

View File

@ -1,3 +1,7 @@
# REDCapCAST 25.2.1
* FIX: `as_factor()` now interprets empty variables with empty levels attribute as logicals to avoid returning factors with empty levels.
# REDCapCAST 25.1.1 # REDCapCAST 25.1.1
The newly introduced extension of `forcats::fct_drop()` has been corrected to work as intended as a method. The newly introduced extension of `forcats::fct_drop()` has been corrected to work as intended as a method.

View File

@ -7,6 +7,8 @@
#' Please refer to parent functions for extended documentation. #' Please refer to parent functions for extended documentation.
#' To avoid redundancy calls and errors, functions are copy-pasted here #' To avoid redundancy calls and errors, functions are copy-pasted here
#' #'
#' Empty variables with empty levels attribute are interpreted as logicals
#'
#' @param x Object to coerce to a factor. #' @param x Object to coerce to a factor.
#' @param ... Other arguments passed down to method. #' @param ... Other arguments passed down to method.
#' @param only_labelled Only apply to labelled columns? #' @param only_labelled Only apply to labelled columns?
@ -24,7 +26,14 @@
#' labels = c(Unknown = 9, Refused = 10), #' labels = c(Unknown = 9, Refused = 10),
#' class = "haven_labelled" #' class = "haven_labelled"
#' ) |> #' ) |>
#' as_factor() #' as_factor() |> class()
#' structure(rep(NA,10),
#' class = c("labelled")
#' ) |>
#' as_factor() |> summary()
#'
#' rep(NA,10) |> as_factor()
#'
#' @importFrom forcats as_factor #' @importFrom forcats as_factor
#' @export #' @export
#' @name as_factor #' @name as_factor
@ -46,8 +55,6 @@ as_factor.logical <- function(x, ...) {
set_attr(x, labels, overwrite = FALSE) set_attr(x, labels, overwrite = FALSE)
} }
#' @rdname as_factor #' @rdname as_factor
#' @export #' @export
as_factor.numeric <- function(x, ...) { as_factor.numeric <- function(x, ...) {
@ -121,7 +128,13 @@ as_factor.haven_labelled <- function(x, levels = c("default", "labels", "values"
x <- structure(x, label = label) x <- structure(x, label = label)
set_attr(x, labels_all, overwrite = FALSE) out <- set_attr(x, labels_all, overwrite = FALSE)
if (all_na(out) & length(levels(out))==0){
as_factor.logical(out)
} else {
out
}
} }
#' @export #' @export

View File

@ -63,6 +63,8 @@ ta loss in case of rich formatted and labelled data.
\details{ \details{
Please refer to parent functions for extended documentation. Please refer to parent functions for extended documentation.
To avoid redundancy calls and errors, functions are copy-pasted here To avoid redundancy calls and errors, functions are copy-pasted here
Empty variables with empty levels attribute are interpreted as logicals
} }
\examples{ \examples{
# will preserve all attributes # will preserve all attributes
@ -77,5 +79,12 @@ structure(c(1, 2, 3, 2, 10, 9),
labels = c(Unknown = 9, Refused = 10), labels = c(Unknown = 9, Refused = 10),
class = "haven_labelled" class = "haven_labelled"
) |> ) |>
as_factor() as_factor() |> class()
structure(rep(NA,10),
class = c("labelled")
) |>
as_factor() |> summary()
rep(NA,10) |> as_factor()
} }