Skip to content

Instantly share code, notes, and snippets.

@DavisVaughan
Created August 24, 2017 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavisVaughan/7ff5125d037397a8f2cd7beb07b0e205 to your computer and use it in GitHub Desktop.
Save DavisVaughan/7ff5125d037397a8f2cd7beb07b0e205 to your computer and use it in GitHub Desktop.
library(rlang)
library(tibble, warn.conflicts = FALSE)
# A formula for the cars dataset
f <- as.formula("speed ~ dist")
f
#> speed ~ dist
#> <environment: 0x7f8399800d80>
# X
X <- model.matrix(f, data = cars)
head(X)
#> (Intercept) dist
#> 1 1 2
#> 2 1 10
#> 3 1 4
#> 4 1 22
#> 5 1 16
#> 6 1 10
# Y - but as a vector, not as a data.frame with a column name
Y <- model.response(model.frame(f, data = cars))
head(Y)
#> 1 2 3 4 5 6
#> 4 4 7 7 8 9
# When you take tidy eval too far
Y <- tibble(!! f_lhs(f) := Y)
head(Y)
#> # A tibble: 6 x 1
#> speed
#> <dbl>
#> 1 4
#> 2 4
#> 3 7
#> 4 7
#> 5 8
#> 6 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment