Michael Gruenstaeudl Michael Gruenstaeudl - 3 months ago 9
R Question

Concatenation of formulae in R

Function

constrain
of the R package diversitree takes a list of formulae as input.

formulae <- list(lambda1 ~ lambda0, mu1 ~ mu0, q10 ~ q01)
constrain(lik, formulae=formulae)


I would like to pass these formulae via a decision tree and concatenate them as needed.

f1 <- "lambda1 ~ lambda0"
f2 <- "mu1 ~ mu0"
f3 <- "q10 ~ q01"


How do I arrive at the above-shown list
formulae
?

Unsuccessful attempt:

formulae <- as.formula(paste(f1,f2,f3, collapse=","))


EDIT 1:

I do not know the precise number of respective formulae a prior, but let them be determined via a decision tree. The precise number of individual formulae (i.e.,
f1
,
f2
,
f3
, etc.) that goes into variable
formulae
should thus not be hard-coded.

Answer

You can use:

formulae = list(as.formula(f1),as.formula(f2),as.formula(f3))

If you originally have all string formulae in a vector, say f <- c(f1, f2, f3), you may use

lapply(f, as.formula)