Introduces NA as additional level for a factor or all factors in a data.frame. Missing values are recoded to the new level.

NAasLevel(X, except = NULL, NA_LEVEL = "NA")

Arguments

X

is either factorial variable or a data.frame

except

=NULL colnames of X (if data.frame) that should not be transformed

NA_LEVEL

="NA" defines the name of the NA level

Value

the transformed factor or data.frame

See also

Examples

if (FALSE) { # test NAasLevel df <- data.frame( x = factor(c("alpha","beta","alpha",NA), levels=c("alpha","beta","gamma")), y = c(5,8,2,1), z = factor(c("red","green","green",NA), levels=c("red","green","blue")), w = c("EGON","DETLEF","DETLEF","DETLEF") ) df NAasLevel(df$x) NAasLevel(df$y) NAasLevel(df$w) NAasLevel(df) NAasLevel(df,NA_LEVEL="missing") # other NA_LEVEL name NAasLevel(df,except="z") # z remains unchanged }