Suppose I have a dataframe my_df and want to pass a column called column_name1 into a function and use that column inside the function with mutate().
My goal is to compare the value in "passed_column" to the value of threshold (sample code below). My code creates the new column but doesn't evaluate the comparison in the ifelse() correctly. I'd like to use the lazyeval package but am open to any solution.
calculate_foo <- function(df, passed_column, threshold){
new_df <- df %>% mutate(new_column = ifelse(!!quo_name(passed_column) >= threshold, 1, 0))
return(new_df)
}
set.seed(2018)
my_df <- data.frame(column_name1 = sample(0:50, 50, replace = T), column_name2 = sample(0:25, 50, replace = T))
threshold_number <- 21
calculate_foo(my_df, "column_name1", threshold_number)