## Problem

You want to extract parts of a formula for further use.

## Solution

You can index into the formula object as though it were a list, using the `[[` operator.

``````f <- y ~ x1 + x2

# Take a look at f
str(f)
#> Class 'formula'  language y ~ x1 + x2
#>   ..- attr(*, ".Environment")=<environment: 0x1e46710>

# Get each part
f[]
#> `~`
f[]
#> y
f[]
#> x1 + x2

# Or view the whole thing as a list
as.list(f)
#> []
#> `~`
#>
#> []
#> y
#>
#> []
#> x1 + x2
#>
#> <environment: 0x1e46710>
``````

For formulas that have nothing on the left side, there are only two elements:

``````f2 <- ~ x1 + x2
as.list(f2)
#> []
#> `~`
#>
#> []
#> x1 + x2
#>
#> <environment: 0x1e46710>
``````

Each of the elements of the formula is an symbol or language object (which consists of multiple symbols:

``````str(f[])
#>  symbol ~
str(f[])
#>  symbol y
str(f[])
#>  language x1 + x2

# Look at parts of the langage object
str(f[][])
#>  symbol +
str(f[][])
#>  symbol x1
str(f[][])
#>  symbol x2
``````

You can use `as.character()` or `deparse()` to convert any of these to strings. `deparse()` can give a more natural-looking result:

``````as.character(f[])
#>  "~"
as.character(f[])
#>  "y"

# The language object gets coerced into a string that represents the parse tree:
as.character(f[])
#>  "+"  "x1" "x2"

# You can use deparse() to get a more natural looking string
deparse(f[])
#>  "x1 + x2"
deparse(f)
#>  "y ~ x1 + x2"
``````

The formula object also captures the environment in which it was called, as we saw earlier when we ran `str(f)`. To extract it, use `environment()`:

``````environment(f)
#> <environment: 0x1e46710>
``````