R/NAtoCategory.r

Defines functions NAtoCategory

Documented in NAtoCategory

NAtoCategory <- function(fact, label = "missing"){
    if (sum(is.na(fact)) > 0){
        fact <- as.factor(fact)
        leve <- levels(fact)
        leve <- leve[leve != ""]        
        res <- rep(NA, length(fact))
        for (i in 1:length(leve)){res[fact == leve[i]] <- i}
        res[is.na(res) == TRUE] <- length(leve) + 1
        res <- factor(res, levels = 1:(length(leve) + 1), labels = c(leve, 
            label))} else {res <- fact}
    return(res)
}

Try the reporttools package in your browser

Any scripts or data that you put into this service are public.

reporttools documentation built on Oct. 12, 2021, 5:06 p.m.