r - How to pass an expression "from higher level" to mutate? -
i create higher level function wrap mutate. want give expression parameter function , being able use expression in mutate :
datas <- data.frame(x = sample(100)) fn <- function(datas, expr) { mutate(datas, flag = eval(substitute(expr), datas)) } fn(datas[1], x > 50) error in mutate_impl(.data, dots) : object 'x' not found
but don't understand why fails since mutate(datas, flag = eval(substitute(x > 50), datas))
works.
what doing wrong ?
thanks
try this:
fn <- function(df, expr) eval(substitute(mutate(df, expr), list(expr=substitute(expr))))
or (preferably) this:
fn <- function(df, expr) mutate_(df, .dots= list(flag=substitute(expr)))
Comments
Post a Comment