I am looking for a way to create an expression that is the product of two given expressions. For example, suppose I have
e1 <- expression(a+b*x)
e2 <- expression(c+d*x)
Now I want to create programatically the expression (e1)*(e2)
:
expression((a+b*x)*(c+d*x))
Background
I am writing a model fitting function. The model has two pieces that are user-defined. I need to be able to "handle" them separately, and then create a combined expression and "handle" it as one model. "Handling" involves taking numeric derivatives, and the deriv
function wants expressions as an input.
I don't deal with this too often but something like this seems to be working
e1 <- expression(a + b*x)
e2 <- expression(c + d*x)
substitute(expression(e1*e2), list(e1 = e1[[1]], e2 = e2[[1]]))
# expression((a + b * x) * (c + d * x))
Try this:
e1 <- quote(a+b*x) # or expression(*)[[1]]
e2 <- quote(c+d*x)
substitute(e1 * e2, list(e1=e1, e2=e2))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With