1

I want to call filter(df, col_name == value) such as filter(mpg, hwy == 27), but

df <- mpg
col_name <- 'hwy'
value <- 27
col <- df[col_name]
filter(df, col == value)

yields the error

Error: Problem with filter() input ..1. i Input ..1 is col == value. x object 'value' not found Run rlang::last_error() to see where the error occurred.

How can I correct col_name <- df[col_name] so that it doesn't produce this error? I've noticed that typeof() and class() produce different results for col (list; tbl_df, tbl, data.frame) and mpg$df (int;int). How do I change col (an object of class tibble and type list) to the same type as mpg$df (int)?


rlang::last_error() gave:

<error/dplyr_error> Problem with filter() input ..1.

i Input ..1 is .data[["hwy"]] == value.

x Column hwy not found in .data

Backtrace:

Run rlang::last_trace() to see the full context.

Mark
  • 389
  • 1
  • 7
  • 19
  • `df[[col_name]]` See [Difference between `[` and `[[`](https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-el) – Rui Barradas Jun 10 '21 at 16:25
  • df[[col_name]] is also not working for me – Mark Jun 10 '21 at 16:29
  • It works with me, the return value is a df with 14 rows and 11 columns. – Rui Barradas Jun 10 '21 at 16:34
  • I assume from the error message that you're using `dplyr::filter`, but it's good to note where all your non-base R functions come from – camille Jun 10 '21 at 16:56

1 Answers1

1

We could use .data

library(dplyr)
filter(df,  .data[[col_name]] == value)

-output

# A tibble: 14 x 11
   manufacturer model        displ  year   cyl trans      drv     cty   hwy fl    class     
   <chr>        <chr>        <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr>     
 1 audi         a4             3.1  2008     6 auto(av)   f        18    27 p     compact   
 2 audi         a4 quattro     2    2008     4 auto(s6)   4        19    27 p     compact   
 3 chevrolet    malibu         2.4  1999     4 auto(l4)   f        19    27 r     midsize   
 4 hyundai      sonata         2.4  1999     4 manual(m5) f        18    27 r     midsize   
 5 hyundai      tiburon        2    2008     4 auto(l4)   f        20    27 r     subcompact
 6 nissan       altima         2.4  1999     4 auto(l4)   f        19    27 r     compact   
 7 nissan       altima         3.5  2008     6 manual(m6) f        19    27 p     midsize   
 8 pontiac      grand prix     3.8  1999     6 auto(l4)   f        17    27 r     midsize   
 9 subaru       forester awd   2.5  2008     4 manual(m5) 4        20    27 r     suv       
10 subaru       impreza awd    2.5  2008     4 auto(s4)   4        20    27 r     compact   
11 subaru       impreza awd    2.5  2008     4 manual(m5) 4        20    27 r     compact   
12 toyota       camry          2.2  1999     4 auto(l4)   f        21    27 r     midsize   
13 toyota       camry solara   2.2  1999     4 auto(l4)   f        21    27 r     compact   
14 toyota       camry solara   3.3  2008     6 auto(s5)   f        18    27 r     compact   

or convert to symbol and evaluate (!!

filter(df,  !! rlang::sym(col_name) == value)

Note: Here we used

col_name <- "hwy"
akrun
  • 874,273
  • 37
  • 540
  • 662