I can't compete with Gabor's "elegant" solution, but I don't understand why
your original "ugly" approach doesn't work.
g <- function(lambdas, f){
z <- as.character(f)
for(i in seq_along(lambdas))
z[3]<- sub(paste0("lambdas[", i, "]"), lambdas[i], z[3], fixed =
TRUE)
formula(paste
Thanks, Gabor! Very elegant!
> On Oct 23, 2020, at 4:15 PM, Gabor Grothendieck
> wrote:
>
> Recursively walk the formula performing the replacement:
>
> g <- function(e, ...) {
>if (length(e) > 1) {
>if (identical(e[[2]], as.name(names(list(...) {
> e <- eval(e, list(
Recursively walk the formula performing the replacement:
g <- function(e, ...) {
if (length(e) > 1) {
if (identical(e[[2]], as.name(names(list(...) {
e <- eval(e, list(...))
}
if (length(e) > 1) for (i in 1:length(e)) e[[i]] <- Recall(e[[i]], ...)
}
Suppose I have a formula like this:
f <- y ~ qss(x, lambda = lambdas[1]) + qss(z, lambdas[2]) + s
I’d like a function, g(lambdas, f) that would take g(c(2,3), f) and produce
the new
formula:
y ~ qss(x, lambda = 2) + qss(z, 3) + s
For only two qss terms I have been using
4 matches
Mail list logo