I have the following data.table
:
dtable <- data.table(column1 = c(1, 2, 3, 5, 6, 7, 8),
column2 = c(1, 1, 1, 5, 5, 6, 8),
column3 = c(7, 8, 9, 0, 9, 2, 3))
I would like to make something like the following functionality, but in a parametrized function:
dtable %>%
dplyr::group_by(column1) %>%
dplyr::summarise(Result = ifelse(column1 == column2, "A", "B"))
To do this, I've created the following function:
Test <- function(.df, .columnName, .columnToGroup){
res <- .df %>%
# This line is interpreted correctly
dplyr::group_by_(lazyeval::interp(.columnToGroup, .columnToGroup = as.name(.columnToGroup))) %>%
# This line does not interpret the == condition as a logical one
dplyr::summarise_(Result = ifelse((lazyeval::interp(.columnToGroup == .columnName,
.columnToGroup = as.name(.columnToGroup),
.columnName = as.name(.columnName))),
"A", "B"))
return(res)
}
I'm using the Non-Standard Evaluation functions (group_by_
and summarise_
) in combination with the lazyeval::interp
function, but the ==
condition is not interpreted the right way, and I get the following exeception:
Test(dtable, "column1", "column2")
Error in UseMethod("interp") :
no applicable method for 'interp' applied to an object of class "logical"
I've tried many different combinations (quote
, expr_env
, as.lazy
, etc.) with no luck. Thanks to this great Non-standard evaluation guide I was able to use these lazyeval
functions to evaluate arithmetic expressions before, but I can't find the way to make them to interpret a logical condition in this piece of code.
Any help would be greatly appreciated.