Skip to content
Please note that GitHub no longer supports your web browser.

We recommend upgrading to the latest Google Chrome or Firefox.

Learn more
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
davidaarmstrong opened this issue May 14, 2019 · 3 comments
Closed
Labels
bug

Comments

@davidaarmstrong
Copy link

@davidaarmstrong 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

This comment has been minimized.

Copy link
Owner

@leeper leeper commented May 15, 2019

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

@davidaarmstrong

This comment has been minimized.

Copy link
Author

@davidaarmstrong davidaarmstrong commented May 15, 2019

@leeper

This comment has been minimized.

Copy link
Owner

@leeper 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 in f4ed2ae Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.