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

Unintuitive syntax for selecting non-numeric columns (Placement of "!") #6944

Closed
mohammadsav opened this issue Oct 26, 2023 · 2 comments
Closed

Comments

@mohammadsav
Copy link

mohammadsav commented Oct 26, 2023

Description: I expect the following code to select all non numeric columns in a tibble (It doesn't work):

df %>%
    select(where(!is.numeric))

The code that works is the following:

df %>%
    select(!where(is.numeric))

I feel like the first one is the more appropriate syntax (correct me if I'm wrong). I read the 2 code blocks as:

  1. In df, select where columns are not numeric.
  2. In df, select not where columns are numeric.

Below is a reprex output

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

n <- 100
df <- tibble(
  Sport = c(rep("Baseball",n/2), rep("Basketball",n/2)),
  Ranking = runif(n,min=1,max=10),
  Date = sample(seq(as.Date("2023-01-01"), as.Date("2023-12-31"), by="days"), n)
)
sapply(df,class)
#>       Sport     Ranking        Date 
#> "character"   "numeric"      "Date"

# This works, selects numeric only
df %>%
  select(where(is.numeric))
#> # A tibble: 100 × 1
#>    Ranking
#>      <dbl>
#>  1    4.72
#>  2    8.16
#>  3    2.15
#>  4    6.44
#>  5    6.57
#>  6    1.74
#>  7    7.75
#>  8    4.26
#>  9    4.27
#> 10    8.69
#> # ℹ 90 more rows

# This works, selects non-numeric only
df %>%
  select(!where(is.numeric))
#> # A tibble: 100 × 2
#>    Sport    Date      
#>    <chr>    <date>    
#>  1 Baseball 2023-08-19
#>  2 Baseball 2023-02-23
#>  3 Baseball 2023-03-30
#>  4 Baseball 2023-01-08
#>  5 Baseball 2023-08-04
#>  6 Baseball 2023-01-03
#>  7 Baseball 2023-01-19
#>  8 Baseball 2023-10-04
#>  9 Baseball 2023-09-20
#> 10 Baseball 2023-06-02
#> # ℹ 90 more rows
# This does not work
df %>%
  select(where(!is.numeric)) # <------------------------- The Issue
#> Error in `select()`:
#> ! Problem while evaluating `where(!is.numeric)`.
#> Caused by error in `!is.numeric`:
#> ! invalid argument type
#> Backtrace:
#>      ▆
#>   1. ├─df %>% select(where(!is.numeric))
#>   2. ├─dplyr::select(., where(!is.numeric))
#>   3. ├─dplyr:::select.data.frame(., where(!is.numeric))
#>   4. │ └─tidyselect::eval_select(expr(c(...)), data = .data, error_call = error_call)
#>   5. │   └─tidyselect:::eval_select_impl(...)
#>   6. │     ├─tidyselect:::with_subscript_errors(...)
#>   7. │     │ └─rlang::try_fetch(...)
#>   8. │     │   └─base::withCallingHandlers(...)
#>   9. │     └─tidyselect:::vars_select_eval(...)
#>  10. │       └─tidyselect:::walk_data_tree(expr, data_mask, context_mask)
#>  11. │         └─tidyselect:::eval_c(expr, data_mask, context_mask)
#>  12. │           └─tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
#>  13. │             └─tidyselect:::walk_data_tree(new, data_mask, context_mask)
#>  14. │               └─tidyselect:::eval_context(expr, context_mask, call = error_call)
#>  15. │                 ├─tidyselect:::with_chained_errors(...)
#>  16. │                 │ └─rlang::try_fetch(...)
#>  17. │                 │   ├─base::tryCatch(...)
#>  18. │                 │   │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  19. │                 │   │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  20. │                 │   │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  21. │                 │   └─base::withCallingHandlers(...)
#>  22. │                 └─rlang::eval_tidy(as_quosure(expr, env), context_mask)
#>  23. ├─tidyselect::where(!is.numeric)
#>  24. │ └─rlang::as_function(fn)
#>  25. │   └─rlang::is_function(x)
#>  26. └─base::.handleSimpleError(`<fn>`, "invalid argument type", base::quote(!is.numeric))
#>  27.   └─rlang (local) h(simpleError(msg, call))
#>  28.     └─handlers[[1L]](cnd)
#>  29.       └─rlang::abort(msg, call = call, parent = cnd)

Created on 2023-10-26 with reprex v2.0.2

@philibe
Copy link

philibe commented Oct 27, 2023

df %>% select(where(Negate(is.numeric))) works also.

@mohammadsav
Copy link
Author

Thanks @philibe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants