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

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

Closed
colearendt opened this issue Jan 10, 2017 · 0 comments · Fixed by #285
Closed

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

colearendt opened this issue Jan 10, 2017 · 0 comments · Fixed by #285
Labels
bug an unexpected problem or unintended behavior wip work in progress

Comments

@colearendt
Copy link
Contributor

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)
colearendt added a commit to colearendt/tidyr that referenced this issue Mar 24, 2017
Change behavior for complete, expand, crossing
colearendt added a commit to colearendt/tidyr that referenced this issue Mar 24, 2017
Alter complete, expand, crossing to complete empty factors

Address documentation issues
@hadley hadley added the bug an unexpected problem or unintended behavior label Jun 23, 2017
@hadley hadley added the wip work in progress label Nov 16, 2017
hadley pushed a commit that referenced this issue Nov 20, 2017
Alter complete, expand, crossing to complete empty factors

Closes #270, closes #271, closes #276
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior wip work in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants