Reputation: 1732
Let's assume the following data.frame
d <- data.frame(x = 1:10, y = 11:20)
Let's now assume that some user input, leads to the following variable
z <- "x + 0.25 * y"
where z is a character, but intended to be interpreted as a formula.
d$z <- d$x + 0.25 * d$y
For regression, the conversion is straightforward with as.formula(), but here I want it to apply to a DF column. The gas-factory method would be to split and identify each separator and translate them.
Upvotes: 4
Views: 64
Reputation: 887481
We can use eval
and use the environment of 'd'
eval(parse(text = z), d)
#[1] 3.75 5.00 6.25 7.50 8.75 10.00 11.25 12.50 13.75 15.00
Upvotes: 2