Skip to content

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

@openclosure

Description

@openclosure
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions