dplyr programming: unexpected behavior of filter

190 views Asked by At

I am trying to use dplyr in the programming way: filter behavior with quoted variables are not understandable.

After several attempts to analyze the real data I had created a following dummy data.

dt <- data.frame(
  sex = rep(c("F","M"), 50),
  height = runif(100, 1, 1000),
  weight = rep(c(2, 100), 50),
  value = runif(100, 1, 1000 ),
  stringsAsFactors =  FALSE
)



library(dplyr)


wizard_fun_1 <-  function(param1){
  par1 <- enquo(param1)

dt %>% select(height, !!par1)
}

wizard_fun_1("sex")

# as expected
#1    74.875344   F
#2   846.614856   M
#.....


wizard_fun_2 <-  function(param1){
  par1 <- enquo(param1)

  dt %>% select(height, !!par1)  %>%
    filter( (!!par1) == 'M')
}

wizard_fun_2('sex')

#[1] height sex  
# ... zero rows....

What's going wrong? Thank's in advanced for any ideas!

2

There are 2 answers

5
MrFlick On

If you are using enquo, you should be calling your function without quotes. For example

wizard_fun_2(sex)

will work just fine. The select function can take strings or symbols. That is both of these will work

select(dt, sex) # more common
select(dt, "sex")

But that's not the same for filter()

filter(sex=="M")
filter("sex"=="M")

So be careful when jumping between strings and unquoted symbols/names. When you are using quote stings, you're not using non-standard evaluation at all really.

0
austensen On

In the function you are using enquo, but then when you call the function you pass the column name as a string rather than the bare name. You just need to use the bare column name when calling the function and it works as written.


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

dt <- tibble(
  sex = rep(c("F","M"), 50),
  height = runif(100, 1, 1000),
  weight = rep(c(2, 100), 50),
  value = runif(100, 1, 1000 )
)


wizard_fun_2 <-  function(param1){
  par1 <- enquo(param1)

  dt %>% select(height, !!par1)  %>%
    filter( (!!par1) == "M")
}

wizard_fun_2(sex)

#> # A tibble: 50 x 2
#>      height   sex
#>       <dbl> <chr>
#>  1 871.7788     M
#>  2 467.9220     M
#>  3 272.6478     M
#>  4 445.1101     M
#>  5 682.2095     M
#>  6 831.8522     M
#>  7 727.9525     M
#>  8 203.7829     M
#>  9 742.3000     M
#> 10 322.0473     M
#> # ... with 40 more rows