Skip to content
Prev 345916 / 398502 Next

Formula with dynamic number of variables

# fixed formula part:
f <- dat ~ a0 * exp(-S*(x - 250)) + K
# convert to character
f <- as.character(f)
# component:
C <- "(p0*exp(-0.5*((x-p1)/p2)^2))"
# number of components (defined randomly):
n <- sample(1:3, 1)
C <- rep(C, n)
# collapse:
C <- paste(C, collapse = "+")
# combine
f <- paste(f[2], f[1], f[3], "+", C)
as.formula(f)
# hope this helps.
# best, s

On 21 November 2014 at 14:23, philippe massicotte
<pmassicotte at hotmail.com> wrote: