tern::df_explicit_na
with a simpler hermes functionFor the example, we use the colData
from the summarized_experiment
object in the hermes
package. We add one missing value to the categorical variable AGE18
, add one missing value to the continuous variable BAGE
, and one missing value to the logical variable LowDepthFlag
.
library(hermes) dat <- colData(summarized_experiment) dat[1, 4] <- NA dat[1, 59] <- NA dat[1, 86] <- NA
We use forcats::fct_explicit_na()
function to add a factor level (Missing)
. This skips using the df_explicit_na
functions in tern
.
We use lapply(, factor)
to convert character variables into factors without using loop, which is used in tern::df_explicit_na
.
hermes_explicit_na <- function(data, na_level = "<Missing>") { assert_that(is(data, "DataFrame")) var_is_logical <- sapply(data, is.logical) data[,var_is_logical] <- lapply(data[,var_is_logical], as.character) var_is_character <- sapply(data, is.character) data[,var_is_character] <- lapply(data[,var_is_character], factor) var_has_missing <- sapply(data, anyNA) var_to_add_NA_level <- var_is_character & var_has_missing data[,var_to_add_NA_level] <- lapply(data[,var_to_add_NA_level], function(s) forcats::fct_explicit_na(s, na_level = na_level) ) data }
A new level (Missing)
is added to AGE18
.
dat_hermes_convert <- hermes_explicit_na(dat) dat_hermes_convert$AGE18
Continuous variable BAGE
does not have any change.
dat_hermes_convert$BAGE
Logical variable is converted to factor. A new level (Missing)
is added.
dat_hermes_convert$LowDepthFlag
Original variable with all records missing is converted to a factor with one level (Missing)
.
dat_hermes_convert$RACE3
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.