Skip to content

arrange_at(vars(...)) chokes if a variable is used for grouping, arrange(...) does not #3332

@mlell

Description

@mlell

The function arrange_at(.) throws an 'object not found' error if a variable is used for sorting which is set as grouping variable.

Examples which work:

mtcars %>% arrange(cyl)
## [Sorted output of mtcars]

# Same meaning, using arrange_at
mtcars %>% group_by(cyl) %>% arrange_at(vars(cyl))
## [Sorted output of mtcars]

However, if the data frame is grouped, only the first version works:

mtcars %>% group_by(cyl) %>% arrange(cyl)
## [Sorted output of mtcars]
# Same meaning,  but does not work:
mtcars %>% group_by(cyl) %>% arrange_at(vars(cyl))
## Error in overscope_eval_next(overscope, expr) : object 'cyl' not found

If a grouped data frame is returned to non-grouped state, both approaches work again:

mtcars %>% group_by(cyl) %>% group_by() %>% arrange_at(vars(-cyl))
## [Sorted output of mtcars]

This is confusing if I want, for example, sort a wide data table containing one name column and many data dimensions by all columns except the name. I need to use

widedata %>% arrange_at(vars(-NameColumn))

If widedata is a result of a prior operation involving grouping, I must explicitly remove the grouping. This would be OK, if technically needed but I think at least the error message

Error in overscope_eval_next(overscope, expr) : object '.....' not found

should be replaced by something like

Error: cannot arrange_by by a column used for grouping

Or, if technically feasible, of course it would be more awesome if arrange_at did not care about grouping!

> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS: /opt/Bio/R/3.4.3/lib64/R/lib/libRblas.so
LAPACK: /opt/Bio/R/3.4.3/lib64/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

other attached packages:
[1] bindrcpp_0.2 dplyr_0.7.4

loaded via a namespace (and not attached):
 [1] compiler_3.4.3   magrittr_1.5     assertthat_0.2.0 R6_2.2.2
 [5] cli_1.0.0        pillar_1.1.0     glue_1.2.0       tibble_1.4.1
 [9] crayon_1.3.4     Rcpp_0.12.15     utf8_1.1.3       pkgconfig_2.0.1
[13] rlang_0.1.6      bindr_0.1

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