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

Dplyr grouped column attribute causes "unknown column" error in unite() #159

Closed
openclosure opened this Issue Jan 26, 2016 · 1 comment

Comments

Projects
None yet
2 participants
@openclosure

openclosure commented Jan 26, 2016

library(dplyr)
library(tidyr)
test <- data.frame(x = c("A", "B", "C"), y = 1, z = c(1, 2, 3)) %>% 
    group_by(x, y, z) %>%
    summarize(count = n())

# Doesn't work, can't find x
test %>% unite(newcol, x, z)
str(test)
# Notice for count, there is an attribute 'vars' with all grouped variable names

# Null out the attributes
attr(test, 'vars') <- NULL
str(test)

# Works
test %>% unite(newcol, x, z)

Can't figure out if this is a bug in dplyr or a bug in tidyr. I think this bug tidyverse/dplyr#859 is related and may not have been fixed in tidyr even though filter now works fine. Sorry if this is already known, I couldn't find any other mentions of this issue.

Edit: It appears that

test %>% tbl_df %>% unite(newcol, x, z) 

fixes the issue. However, the only thing this does is remove 'grouped_df' from the list of classes of the test object. Not sure if this is a bug at this point.

@aammd

This comment has been minimized.

aammd commented Mar 1, 2016

ungroup() also fixes this. It appears that unite() forbids you from modifying a grouping variable. It would be helpful if the error was the same/similar as that given by mutate() when you try to mutate the grouping variable: Error: cannot modify grouping variable

@hadley hadley closed this in 3dbcbf7 May 16, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment