[R] Using update() within a function with a changed formula

Viechtbauer, Wolfgang (SP) wo||g@ng@v|echtb@uer @end|ng |rom m@@@tr|chtun|ver@|ty@n|
Sat Nov 13 20:24:08 CET 2021


Hello all,

Say I would like to change the outcome in a formula to a variable not part of the original dataset. This works just fine:

res <- lm(mpg ~ wt + cyl, data=mtcars)
res
y <- rnorm(nobs(x))
update(x, formula = y ~ .)

But not when doing so within a function:

rm(y)

f <- function(x) {
   y <- rnorm(nobs(x))
   update(x, formula = y ~ .)
}

f(res)

Is there a way to make this work? Using y <<- ... inside the function works, but I would like to avoid such a heavy-handed approach.

Best,
Wolfgang



More information about the R-help mailing list