Skip to content

complete() does not use factor levels if no data #270

@colearendt

Description

@colearendt

complete() has behavior advertised as follows:

For factors, the full set of levels (not just those that appear in the data) are used.

However, this seems to only be the case so long as you have at least one record. A few workarounds are less than optimal in my opinion, and it would be quite helpful for this functionality to work with an empty data.frame

options <- factor(c('a','b','c'))

df <- data.frame(column=character(), column2=character()) 
levels(df$column) <- options; levels(df$column2) <- options;

## Does not work - though I would expect it to
df %>% complete(column, column2)

## Works - nice if you have options readily available
df %>% complete(column=options, column2=options)

## Works - based on factors, and a bit tedious
df %>% complete(column=factor(levels(column),levels=levels(column)),column2=factor(levels(column),levels=levels(column)))



## With data, things work fine:
df <- data.frame(column=c('a'),column2=c('b'))
levels(df$column) <- options; levels(df$column2) <- options;

## Does not work above - works here
df %>% complete(column, column2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorwipwork in progress

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions