# Get list of columns from formula

**URL:** <https://discourse.julialang.org/t/get-list-of-columns-from-formula/60611>\
**Category:** Statistics\
**Tags:** statsmodels\
**Created:** [May 6, 2021, 12:48am UTC](https://discourse.julialang.org/t/get-list-of-columns-from-formula/60611 "2021-05-06T00:48:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [May 6, 2021, 12:48am UTC](https://discourse.julialang.org/t/get-list-of-columns-from-formula/60611/1 "2021-05-06T00:48:07Z")

</div>

I want to make a dataframe that has only the columns that appear in the formula. How can I get the list of terms from a formula?

```julia
@formula(y ~ a + b + c * d + e + e ^ 2)

```

* * *

cc @dave.f.kleinschmidt

---

<div class="post-metadata">

**Author:** ![dave.f.kleinschmidt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dave.f.kleinschmidt/32/55_2.png) [@dave.f.kleinschmidt](https://discourse.julialang.org/u/dave.f.kleinschmidt)\
**Post date:** [May 6, 2021, 6:33pm UTC](https://discourse.julialang.org/t/get-list-of-columns-from-formula/60611/2 "2021-05-06T18:33:09Z")

</div>

There’s an [internal function called `termvars`](https://github.com/JuliaStats/StatsModels.jl/blob/0d0c2bbd56b183491e8c831a92168d4265e4ea68/src/schema.jl#L387-L398) that should do it, something like:

```julia
f = @formula(y ~ a + b + c * d + e + e ^ 2)
f_data = select(data, StatsModels.termvars(f))

```

That’s what we currently use to get the table columns in [https://github.com/JuliaStats/StatsModels.jl/blob/0d0c2bbd56b183491e8c831a92168d4265e4ea68/src/modelframe.jl#L70](https://github.com/JuliaStats/StatsModels.jl/blob/0d0c2bbd56b183491e8c831a92168d4265e4ea68/src/modelframe.jl#L70)
