Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable Labels in Factorize and Characterize [Suggested Code Change] #204

Closed
1 task done
davidaarmstrong opened this issue May 14, 2019 · 3 comments
Closed
1 task done
Labels

Comments

@davidaarmstrong
Copy link

davidaarmstrong commented May 14, 2019

Please specify whether your issue is about:

  • a suggested code or documentation change, improvement to the code, or feature request

Would it be possible to change the characterize and factorize functions as follows? This will propagate the variable label through to the newly created variable (if it exists).

I also added a couple of lines to factorize that would omit missing values in the label table that identify original responses that were coded as missing.

characterize.default <- function(x, ...) {
    xlab <- NULL
    if(!is.null(attributes(x)$label)){
        xlab <- attributes(x)$label
    }
    
    if (!is.null(attributes(x)$labels)) {
        x <- as.character(factorize(x, ...))
        if(!is.null(xlab)){
            attr(x, "label") <- xlab
        }
        x
    } else {
        x
    }
}

and

factorize.default <- function(x, ...) {
    xlab <- NULL
    if(!is.null(attributes(x)$label)){
        xlab <- attributes(x)$label
    }
    if (!is.null(attributes(x)$labels)) {
        labs <- na.omit(attributes(x)$labels)
        x <- factor(x, labs, names(labs), ...)
        if(!is.null(xlab)){
            attr(x, "label") <- xlab
        }
        x
    } else {
        x
    }
    
}```
@leeper
Copy link
Contributor

leeper commented May 15, 2019

Can you show me a before and after on what the difference is in output?

@davidaarmstrong
Copy link
Author

davidaarmstrong commented May 15, 2019 via email

@leeper
Copy link
Contributor

leeper commented May 24, 2019

Got it. Thanks. Yes, this is a bug - appreciate the suggested fix!

@leeper leeper added the bug label May 24, 2019
@leeper leeper closed this as completed in f4ed2ae Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants