Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lorenzwalthert/e16048efdd2e02e0e0eee59f6966879a to your computer and use it in GitHub Desktop.
Save lorenzwalthert/e16048efdd2e02e0e0eee59f6966879a to your computer and use it in GitHub Desktop.
formula
``` r
string_formula <- paste0(names(mtcars)[1], " ~", paste0(names(mtcars)[-1], collapse = "+"))
lm(as.formula(string_formula), data = mtcars)
#>
#> Call:
#> lm(formula = as.formula(string_formula), data = mtcars)
#>
#> Coefficients:
#> (Intercept) cyl disp hp drat
#> 12.30337 -0.11144 0.01334 -0.02148 0.78711
#> wt qsec vs am gear
#> -3.71530 0.82104 0.31776 2.52023 0.65541
#> carb
#> -0.19942
mtcars$blablabla <- 3
lm(as.formula(string_formula), data = mtcars)
#>
#> Call:
#> lm(formula = as.formula(string_formula), data = mtcars)
#>
#> Coefficients:
#> (Intercept) cyl disp hp drat
#> 12.30337 -0.11144 0.01334 -0.02148 0.78711
#> wt qsec vs am gear
#> -3.71530 0.82104 0.31776 2.52023 0.65541
#> carb
#> -0.19942
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment