Skip to content

Commit

Permalink
selects.grouped_df + non-syntactic variables
Browse files Browse the repository at this point in the history
Fixes #1138
  • Loading branch information
hadley committed Mar 8, 2016
1 parent 12b910f commit 41a666d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
@@ -1,5 +1,8 @@
# dplyr 0.4.3.9000

* `select()` works even if the grouping variable has a non-syntactic name
(#1138).

* In `select()`, negating a failed match (e.g. `select(mtcars, -contains("x"))`)
returns all columns, instead of no columns (#1176)

Expand Down
3 changes: 2 additions & 1 deletion R/grouped-df.r
Expand Up @@ -95,7 +95,8 @@ select_.grouped_df <- function(.data, ..., .dots) {
vars <- select_vars_(names(.data), dots)

# Ensure all grouping variables are present, notifying user with a message
missing <- setdiff(as.character(groups(.data)), vars)
group_names <- vapply(groups(.data), as.character, character(1))
missing <- setdiff(group_names, vars)

if (length(missing) > 0) {
message("Adding missing grouping variables: ",
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-select.r
Expand Up @@ -24,6 +24,11 @@ test_that("grouping variables preserved with a message (#1511)", {
expect_named(res, c("g", "x"))
})

test_that("non-syntactic grouping variable is preserved (#1138)", {
df <- data_frame(`a b` = 1L) %>% group_by(`a b`) %>% select()
expect_named(df, "a b")
})

test_that("select doesn't fail if some names missing", {
df1 <- data.frame(x = 1:10, y = 1:10, z = 1:10)
df2 <- setNames(df1, c("x", "y", ""))
Expand Down

0 comments on commit 41a666d

Please sign in to comment.