# Get columns of DataFrames.jl ModelMatrix object

**URL:** <https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780>\
**Category:** Statistics\
**Created:** [March 20, 2017, 5:33pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780 "2017-03-20T17:33:36Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbohor3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sbohor3](https://discourse.julialang.org/u/sbohor3)\
**Post date:** [March 20, 2017, 5:33pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/1 "2017-03-20T17:33:36Z")

</div>

Is there a way to extract column names of an object created from ModelMatrix() so that I can use those columns as row names for coefficients?  
Thanks.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 20, 2017, 6:02pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/2 "2017-03-20T18:02:24Z")

</div>

> [@sbohor3](#):
>
> object created from ModelMatrix()

This isn’t standard Julia. Is this part of some package?

---

<div class="post-metadata">

**Author:** ![sbohor3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sbohor3](https://discourse.julialang.org/u/sbohor3)\
**Post date:** [March 20, 2017, 6:29pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/3 "2017-03-20T18:29:20Z")

</div>

Actually, I am writing a Julia function for a stat method in which I need to use ModelMatrix and would like to obtain the levels of the covariates from the ModelMatrix so I can use them to name the coefficients in the output.

Thanks.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 20, 2017, 6:31pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/4 "2017-03-20T18:31:05Z")

</div>

Oh, you should’ve mentioned that this is a DataFrames.jl thing.

[http://juliastats.github.io/DataFrames.jl/latest/man/formulas/#The-Formula,-ModelFrame-and-ModelMatrix-Types-1](http://juliastats.github.io/DataFrames.jl/latest/man/formulas/#The-Formula,-ModelFrame-and-ModelMatrix-Types-1)

Edited for clarity.

---

<div class="post-metadata">

**Author:** ![sbohor3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sbohor3](https://discourse.julialang.org/u/sbohor3)\
**Post date:** [March 20, 2017, 6:41pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/5 "2017-03-20T18:41:12Z")

</div>

Oh sorry, yes I was referring to ModelMatrix in DataFrame.jl. I cannot obtain the column names from an object created from ModelMatrix(). Any idea?  
Thanks.  
So

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [March 20, 2017, 7:23pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/6 "2017-03-20T19:23:53Z")

</div>

You don’t have access to the names at the `ModelMatrix` stage. You’d need the `ModelFrame` for that.

---

<div class="post-metadata">

**Author:** ![sbohor3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sbohor3](https://discourse.julialang.org/u/sbohor3)\
**Post date:** [March 20, 2017, 7:26pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/7 "2017-03-20T19:26:21Z")

</div>

Thanks. Would `ModelFrame` give me columns by creating dummy indicators of levels of factor variables? Thanks,

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [March 20, 2017, 7:44pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/8 "2017-03-20T19:44:18Z")

</div>

No that is what `ModelMatrix` does but from a `mf::ModelFrame` you can get the `ModelMatrix` with `ModelMatrix(mf)` and the column names with `coefnames(mf)`.

---

<div class="post-metadata">

**Author:** ![sbohor3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sbohor3](https://discourse.julialang.org/u/sbohor3)\
**Post date:** [March 20, 2017, 7:46pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/9 "2017-03-20T19:46:06Z")

</div>

@andreasnoack @ChrisRackauckas : Below is an example of the same problem addressed in R language. Notice how levels (1s) of factor variables are attached to the column variables `am` and `vs`. I would like to do the same in Julia so that I can access the names (columns) from design matrix. Thanks.

```r
> table(df)
   vs
am 0 1
  0 2 5
  1 2 1

> model.matrix(~., df)
   (Intercept) am1 vs1
1 1 1 0
2 1 1 0
3 1 1 1
4 1 0 1
5 1 0 0
6 1 0 1
7 1 0 0
8 1 0 1
9 1 0 1
10 1 0 1
attr(,"assign")
[1] 0 1 2
attr(,"contrasts")
attr(,"contrasts")$am
[1] "contr.treatment"

attr(,"contrasts")$vs
[1] "contr.treatment"

```

---

<div class="post-metadata">

**Author:** ![sbohor3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sbohor3](https://discourse.julialang.org/u/sbohor3)\
**Post date:** [March 20, 2017, 9:01pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/10 "2017-03-20T21:01:19Z")

</div>

@andreasnoack: Thanks a lot. That’s what I wanted.

---

<div class="post-metadata">

**Author:** ![sbohor3](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@sbohor3](https://discourse.julialang.org/u/sbohor3)\
**Post date:** [March 22, 2017, 8:27pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/11 "2017-03-22T20:27:04Z")

</div>

@andreasnoack: Is there any way to boost the speed of `Pkg.add()` or `using packages`? These are too slow. Thanks.

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [March 22, 2017, 8:49pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/12 "2017-03-22T20:49:59Z")

</div>

Not really at this point but it might get better soon. See [https://github.com/JuliaLang/Juleps/blob/master/Pkg3.md](https://github.com/JuliaLang/Juleps/blob/master/Pkg3.md) though it is not certain when it will arrive.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 24, 2017, 5:13pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/13 "2017-03-24T17:13:14Z")

</div>

12 posts were split to a new topic: [How to start writing a new Julia package?](https://discourse.julialang.org/t/how-to-start-writing-a-new-julia-package/2864)

---

<div class="post-metadata">

**Author:** ![datnamer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datnamer/32/3471_2.png) [@datnamer](https://discourse.julialang.org/u/datnamer)\
**Post date:** [March 23, 2017, 5:36pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/14 "2017-03-23T17:36:32Z")

</div>

Is that a 1.0 feature?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 24, 2017, 5:14pm UTC](https://discourse.julialang.org/t/get-columns-of-dataframes-jl-modelmatrix-object/2780/15 "2017-03-24T17:14:58Z")

</div>

A post was merged into an existing topic: [How to start writing a new Julia package?](https://discourse.julialang.org/t/how-to-start-writing-a-new-julia-package/2864)
