r - Passing parameters to with() via function -
there way pass parameter function with()
? like:
dados <- data.frame(x=1:10, v1=rnorm(10), v2=rnorm(10)) with(dados, v1+v2) # works func <- function(data, funcao) { with(data, funcao) } func(dados, v1+v2) # fails func(dados, 'v1+v2') # fails
i've tried eval()
, fails :/
ok, think got it. need call eval inside func , pass expression:
dados <- data.frame(x=1:10, v1=rnorm(10), v2=rnorm(10)) func <- function(data, funcao) { with(data, eval(funcao)) } func(dados, expression(v1+v2)) [1] -0.9950362 1.0934899 -0.9791810 -1.2420633 -1.0930204 0.8941630 -2.3307571 -1.5012386 3.2731584 0.2585419
to use string:
x = "v1 + v2" func(dados, parse(text=x)) [1] -0.9950362 1.0934899 -0.9791810 -1.2420633 -1.0930204 0.8941630 -2.3307571 -1.5012386 3.2731584 0.2585419
Comments
Post a Comment