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

Comments

Projects
None yet
2 participants
@colearendt
Contributor

colearendt commented Jan 10, 2017

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

Close tidyverse#270, add motivational tests
Change behavior for complete, expand, crossing

colearendt added a commit to colearendt/tidyr that referenced this issue Mar 24, 2017

Close tidyverse#270, close tidyverse#271, close tidyverse#276
Alter complete, expand, crossing to complete empty factors

Address documentation issues

@hadley hadley added the bug label Jun 23, 2017

@hadley hadley added the wip label Nov 16, 2017

@hadley hadley closed this in #285 Nov 20, 2017

hadley added a commit that referenced this issue Nov 20, 2017

Complete Empty Factors (#285)
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