Skip to content
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

Scoped rename functions don't work on grouped_df #2947

Closed
echasnovski opened this issue Jul 6, 2017 · 8 comments
Closed

Scoped rename functions don't work on grouped_df #2947

echasnovski opened this issue Jul 6, 2017 · 8 comments
Assignees
Labels
bug

Comments

@echasnovski
Copy link
Contributor

@echasnovski echasnovski commented Jul 6, 2017

In dplyr 0.7.1 these work:

mtcars %>% rename_all(toupper)
mtcars %>% group_by(vs) %>% rename(VS = vs)

But these don't (throw an error All arguments must be named):

mtcars %>% group_by(vs) %>% rename_all(toupper)
mtcars %>% group_by(vs) %>% rename_if(rlang::is_integerish, toupper)
mtcars %>% group_by(vs) %>% rename_at("mpg", toupper)
@mdpead
Copy link

@mdpead mdpead commented Aug 13, 2017

I've run into the same problem (in version 0.7.2). I noticed that the scoped select variants work as expected, and given the similarities between select and rename thought that this could help give a clue as to the fix.

So:

mtcars %>% group_by(vs) %>% select_all(toupper)
mtcars %>% group_by(vs) %>% select_if(rlang::is_integerish, toupper)
mtcars %>% group_by(vs) %>% select_at("mpg", toupper)

all work as expected. Sorry I can't be of more assistance, thanks for the awesome package!

@billdenney
Copy link
Contributor

@billdenney billdenney commented Sep 8, 2017

I've seen the same issue with version 0.7.3 (installed about a week ago):

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
obj <- data.frame(A_B = rep(1:2, each = 2), C_D = 1:4)
obj %>% rename_all(function(x) gsub("_", " ", x))
#>   A B C D
#> 1   1   1
#> 2   1   2
#> 3   2   3
#> 4   2   4
obj %>% group_by(A_B) %>% rename_all(function(x) gsub("_", " ", x))
#> Error: All arguments must be named

For the work I'm doing right now, a reasonable work-around is to add a group_by() %>% just before the rename_all call.

@hadley hadley added bug data frame labels Nov 2, 2017
@dpeterson71
Copy link

@dpeterson71 dpeterson71 commented Jan 16, 2018

It's not just grouped or not. The rename function is broken in 0.7.4. Even the iris example in the documentation fails. The select function appears to be OK, though, as observed above. From running "example(rename)":

rename> # Renaming -----------------------------------------
rename> # * select() keeps only the variables you specify
rename> select(iris, petal_length = Petal.Length)

A tibble: 150 x 1

petal_length

1 1.40
2 1.40
3 1.30
4 1.50
5 1.40
6 1.70
7 1.40
8 1.50
9 1.40
10 1.50

... with 140 more rows

rename> # * rename() keeps all variables
rename> rename(iris, petal_length = Petal.Length)
Error: petal_length = Petal.Length must be a symbol or a string, not formula

sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.6

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] graphics grDevices utils datasets stats methods base

other attached packages:
[1] bindrcpp_0.2 lubridate_1.7.1 forcats_0.2.0 stringr_1.2.0
[5] dplyr_0.7.4 purrr_0.2.4 readr_1.1.1 tidyr_0.7.2.9000
[9] tibble_1.4.1 ggplot2_2.2.1.9000 tidyverse_1.2.1 TribridAcquisition_1.4.21.9000
[13] devtools_1.13.4

loaded via a namespace (and not attached):
[1] tidyselect_0.2.3 reshape2_1.4.3 haven_1.1.0 lattice_0.20-35 colorspace_1.3-2 yaml_2.1.16 utf8_1.1.2 rlang_0.1.6.9002
[9] pillar_1.0.99.9000 foreign_0.8-69 glue_1.2.0 withr_2.1.1.9000 modelr_0.1.1 readxl_1.0.0 bindr_0.1 plyr_1.8.4
[17] robustbase_0.92-8 munsell_0.4.3 gtable_0.2.0 cellranger_1.1.0 rvest_0.3.2 psych_1.7.8 memoise_1.1.0 labeling_0.3
[25] knitr_1.18 parallel_3.3.3 DEoptimR_1.0-8 broom_0.4.3 Rcpp_0.12.14 KernSmooth_2.23-15 scales_0.5.0.9000 jsonlite_1.5
[33] mnormt_1.5-5 hms_0.4.0 digest_0.6.13 stringi_1.1.6 grid_3.3.3 cli_1.0.0 tools_3.3.3 magrittr_1.5
[41] lazyeval_0.2.1 crayon_1.3.4 pkgconfig_2.0.1 xml2_1.1.1 assertthat_0.2.0 httr_1.3.1 rstudioapi_0.7 R6_2.2.2
[49] nlme_3.1-131

@batpigandme
Copy link
Member

@batpigandme batpigandme commented Jan 16, 2018

FTR, the examples using ungrouped select and rename, as in @dpeterson71's example, do work in the dev version (I'm currently running tidyverse/dplyr@f49115b f49115b). 👍

library(tidyverse)

# select() keeps only the variables you specify
s <- select(iris, petal_length = Petal.Length)
head(s)
#>   petal_length
#> 1          1.4
#> 2          1.4
#> 3          1.3
#> 4          1.5
#> 5          1.4
#> 6          1.7
# rename() keeps all variables
r <- rename(iris, petal_length = Petal.Length)
head(r)
#>   Sepal.Length Sepal.Width petal_length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa

Created on 2018-01-16 by the reprex package (v0.1.1.9000).

@dpeterson71
Copy link

@dpeterson71 dpeterson71 commented Jan 16, 2018

Following up on this I checked the behavior on two other machines and found the rename function working. The common feature of the working machines was rlang 0.1.6. The broken implementation has the development version of rlang (0.16.9002) installed as seen in the sessionInfo() in my initial comment. I'll file a bug report there.

@batpigandme
Copy link
Member

@batpigandme batpigandme commented Jan 16, 2018

@dpeterson71 it works fine with 0.16.9003, so no need.

@bkayser
Copy link

@bkayser bkayser commented Feb 15, 2018

@dpeterson71 @batpigandme @JohnMount FWIW rlang 0.1.6.9003 did not resolve the issue for me but inserting group_by() did work around the problem.

lionel- added a commit to lionel-/dplyr that referenced this issue Feb 15, 2018
krlmlr pushed a commit that referenced this issue Mar 12, 2018
* Pass arguments directly to vars_rename()

* Fix colwise renaming of variables on grouped data

Closes #2947
@lock
Copy link

@lock lock bot commented Sep 8, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Sep 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug
Projects
None yet
Development

No branches or pull requests

8 participants