Skip to contents

[Experimental]

This utility function converts all eligible character and logical variables in a S4Vectors::DataFrame to factor variables. All factor variables get amended with an explicit missing level. This includes both NA and empty strings.

Usage

df_cols_to_factor(data, omit_columns = NULL, na_level = "<Missing>")

Arguments

data

(DataFrame)
input S4Vectors::DataFrame.

omit_columns

(character or NULL)
which columns should be omitted from the possible conversion to factor and explicit missing level application.

na_level

(string)
explicit missing level to be used for factor variables.

Value

The modified data.

Note

All required rowData and colData variables cannot be converted to ensure proper downstream behavior. These are automatically omitted if found in data and therefore do not need to be specified in omit_columns.

Examples

dat <- colData(summarized_experiment)
any(vapply(dat, is.character, logical(1)))
#> [1] TRUE
any(vapply(dat, is.logical, logical(1)))
#> [1] TRUE
dat_converted <- df_cols_to_factor(dat)
any(vapply(dat_converted, function(x) is.character(x) || is.logical(x), logical(1)))
#> [1] FALSE