filter rows >= certain value of column passed to function

Hello!

I'm trying to create a function that takes a df, column name, and cutoff score as arguments. I'm trying to get the function to filter and then print those rows were {{column}} >= the cutoff score. I'm almost there, but I'm hitting a snag somewhere. Here's what I have so far.


#> The data

data <- as_tibble( data.frame(
  Item1 = sample( c(1, 0), 10, replace = T),
  Item2 = sample( c(1, 0), 10, replace = T),
  Item3 = sample( c(1, 0), 10, replace = T)
))

data2 <- data %>%
  mutate(., Total = rowSums(.)) #> "Total" is a student's total score, for example.

#> The function I'm trying to knock out

analyze_b <- function(df, column, cutoff){ 
  
  df2 <- arrange(df, desc({{column}}))
  
  passers <- df2 %>%
    filter(df2, {{column}} >= {{cutoff}}) #> "passers" are those whose "Total" is >= the "cutoff"

  print(passers)
  
}

analyze_b(data2, "Total", 7) #> The user enters the column name that has students' total scores and 
                                             #> and the cutoff score for their test

I run into trouble when I enter the cutoff score into the function. Here's the error I get.

Error: Problem with filter() input ..1.
x Input ..1$Item1 must be a logical vector, not a double.
i Input ..1 is df2.

Any help? THANK YOU!

There's a problem with your filter() syntax. You are piping df2 and then specifying it again as a subsequent argument. In fact, your code could be simplified to just the following.

library(tidyverse)

set.seed(42)

data <- tibble(Item1 = sample(1:5, 10, replace = T),
               Item2 = sample(1:5, 10, replace = T),
               Item3 = sample(1:5, 10, replace = T)) %>% 
  mutate(Total = rowSums(.))
  
data
#> # A tibble: 10 x 4
#>    Item1 Item2 Item3 Total
#>    <int> <int> <int> <dbl>
#>  1     1     1     5     7
#>  2     5     5     5    15
#>  3     1     4     5    10
#>  4     1     2     4     7
#>  5     2     2     2     6
#>  6     4     3     4    11
#>  7     2     1     3     6
#>  8     2     1     2     5
#>  9     1     3     1     5
#> 10     4     4     2    10

analyze_b <- function(df, column, cutoff) { 
  
  filter(df, {{ column }} >= cutoff)

}

analyze_b(data, Total, 7)
#> # A tibble: 6 x 4
#>   Item1 Item2 Item3 Total
#>   <int> <int> <int> <dbl>
#> 1     1     1     5     7
#> 2     5     5     5    15
#> 3     1     4     5    10
#> 4     1     2     4     7
#> 5     4     3     4    11
#> 6     4     4     2    10

Created on 2020-06-21 by the reprex package (v0.3.0)

2 Likes

Alright! Thanks so much for the help! Exactly what I was looking for. Also, thanks for explaining what was happening with the filter syntax; that helps a bunch, too. Very much appreciated!

Great! If I solved your problem, please consider marking the post as a solution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.