[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)
y <- rnorm(nobs(x))
update(x, formula = y ~ .)

But not when doing so within a function:


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


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.


More information about the R-help mailing list