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

spread() with drop = FALSE applied to var column including NA values results an error #254

Closed
ngr-t opened this Issue Oct 19, 2016 · 2 comments

Comments

Projects
None yet
2 participants
@ngr-t
Contributor

ngr-t commented Oct 19, 2016

Below is an example data.frame.

> (df_with_na <- data.frame(var = factor(c(LETTERS[1:3], NA), LETTERS[1:4]), val = seq(4)))
   var val
1    A   1
2    B   2
3    C   3
4 <NA>   4
> df_with_na$var
[1] A    B    C    <NA>
Levels: A B C D

When I spread this df by the "var" column, call like

> tidyr::spread(df_with_na, var, val)
  A B C <NA>
1 1 2 3    4

In this case, I got an column "" originate from the row that contains NA in "var" column.
Then I added "drop = FALSE" option to keep all levels (including "D"), and got this:

> tidyr::spread(df_with_na, var, val, drop = FALSE)
Error in `colnames<-`(`*tmp*`, value = c("A", "B", "C", "D", "<NA>")) : 
  length of 'dimnames' [2] not equal to array extent

I think this is not an intended behavior and it'd better to return a data.frame with columns for all levels of "var" column and NA.

@hadley

This comment has been minimized.

Member

hadley commented Jun 23, 2017

Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session

@hadley hadley added the reprex label Jun 23, 2017

@ngr-t

This comment has been minimized.

Contributor

ngr-t commented Jun 24, 2017

Is this alright?

# Prepare data.frame                                                                     
(df_with_na <- data.frame(var = factor(c(LETTERS[1:3], NA), LETTERS[1:4]), val = seq(4)))
#>    var val
#> 1    A   1
#> 2    B   2
#> 3    C   3
#> 4 <NA>   4
df_with_na$var                                                                           
#> [1] A    B    C    <NA>
#> Levels: A B C D
# No problem                                                                             
tidyr::spread(df_with_na, var, val)                                                      
#>   A B C <NA>
#> 1 1 2 3    4
# Bug?                                                                                   
tidyr::spread(df_with_na, var, val, drop = FALSE)                                        
#> Error in dimnames(x) <- dn: length of 'dimnames' [2] not equal to array extent

ngr-t added a commit to ngr-t/tidyr that referenced this issue Jun 25, 2017

@ngr-t ngr-t referenced this issue Jun 25, 2017

Merged

Fix #254. #319

ngr-t added a commit to ngr-t/tidyr that referenced this issue Jun 25, 2017

@hadley hadley closed this in #319 Nov 15, 2017

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

Fix #254. (#319)
* Fix #254.

* Mentioned about fix of #254 in NEW.md.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment