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.
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.
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!