Skip to content

crossing() removes NA from factor levels #410

@echasnovski

Description

@echasnovski
library(tidyr)
packageVersion("tidyr")
#> [1] '0.8.0.9000'
x_fac <- factor(c(1, NA), exclude = NULL)
levels(x_fac)
#> [1] "1" NA
x_cross <- crossing(x_fac)
levels(x_cross$x_fac)
#> [1] "1"

The root of this seems to be in ulevels():

ulevels(factor(c(1, NA), exclude = NULL))
#> [1] 1    <NA>
#> Levels: 1

Adding exclude = NULL in factor() inside ulevels() solves the issue (and passes all current tests).

If this is an unintended behavior, I am ready to make a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorpivoting ♻️pivot rectangular data to different "shapes"wipwork in progress

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions