Skip to content

as_factor() turns values into NAs #570

Closed

Description

According to the documentation, when the argument levels = "labels", then "unlabelled values become NA" which is ok.
However, when levels = "values" I would have expected to obtain a factor having all values, labelled or not.

library(haven)
set.seed(1234)
x <- labelled(sample(1:5, 20, replace = TRUE), labels = c(Good = 1, Bad = 5))

as_factor(x, levels = "values")
#  [1] <NA> <NA> 5    <NA> 1    5    <NA> <NA> <NA> <NA> <NA> <NA> 5    <NA> <NA> <NA>
# [17] 5    <NA> 5    <NA>
# Levels: 1 5

If the current functionality is alright, the documentation should mention it drops values without labels (just like the documentation for levels = "labels"), or otherwise allow as_factor() to use all values, which is what I would expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions