(R) How can I force substitution of a vector element in a formula? -
i'm trying use formula expressed in terms of element of vector. element not substituted when formula built; instead left in component---i'd prefer if actual value substituted in immediately. (this breaks else later in code.)
c <- c(5:8) #made vector on purpose, though use c[1] form <- as.formula(y ~ exp(-(x-c[1])^2/2))
at point form
contains y ~ exp(-(x - c[1])^2/(2))
i'd contain y ~ exp(-(x - 5)^2/(2))
.
the bquote
makes pretty easy plug vlaues formulas. example
form <- bquote(y ~ exp(-(x- .( c[1]) )^2/2)) form # y ~ exp(-(x - 5l)^2/2)
note add "l" indicate integer literal value (as opposed general numeric value). because 5:8
creates integer vector.
Comments
Post a Comment