# Why use unevaluated expression to refer to a column name

**URL:** <https://discourse.julialang.org/t/why-use-unevaluated-expression-to-refer-to-a-column-name/59424>\
**Category:** General Usage\
**Tags:** question, mlj\
**Created:** [April 16, 2021, 4:40pm UTC](https://discourse.julialang.org/t/why-use-unevaluated-expression-to-refer-to-a-column-name/59424 "2021-04-16T16:40:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![quazirfan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quazirfan/32/23381_2.png) [@quazirfan](https://discourse.julialang.org/u/quazirfan)\
**Post date:** [April 16, 2021, 4:40pm UTC](https://discourse.julialang.org/t/why-use-unevaluated-expression-to-refer-to-a-column-name/59424/1 "2021-04-16T16:40:20Z")

</div>

I understand that I need to use : operator to represent a variable in an unevaluated expression in Julia. But I am confused as to why I am using : to get column name here - the column name do not appear to be an unevaluated expression.

```
using RDatasets, MLJ
iris = dataset("datasets", "iris")
iris2 = coerce(iris, :Species=> OrderedFactor) # This line

```

Is it possible to refer to a column name here without using unevaluated expression?

Thanks!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 16, 2021, 4:58pm UTC](https://discourse.julialang.org/t/why-use-unevaluated-expression-to-refer-to-a-column-name/59424/2 "2021-04-16T16:58:38Z")

</div>

`:` doesn’t create an unevaluated expression, it creates a `Symbol`, which is a more widely-used type. You can learn more about it here:

```julia
help?> Symbol
search: Symbol

  Symbol

  The type of object used to represent identifiers in parsed julia code (ASTs). Also often used as a name or label to
  identify an entity (e.g. as a dictionary key). Symbols can be entered using the : quote operator:

  julia> :name
  :name
  
  julia> typeof(:name)
  Symbol
  
  julia> x = 42
  42
  
  julia> eval(:x)
  42

  Symbols can also be constructed from strings or other values by calling the constructor Symbol(x...).

  Symbols are immutable and should be compared using ===. The implementation re-uses the same object for all Symbols
  with the same name, so comparison tends to be efficient (it can just compare pointers).

  Unlike strings, Symbols are "atomic" or "scalar" entities that do not support iteration over characters.

```

---

<div class="post-metadata">

**Author:** ![quazirfan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quazirfan/32/23381_2.png) [@quazirfan](https://discourse.julialang.org/u/quazirfan)\
**Post date:** [April 16, 2021, 5:10pm UTC](https://discourse.julialang.org/t/why-use-unevaluated-expression-to-refer-to-a-column-name/59424/3 "2021-04-16T17:10:23Z")

</div>

```julia
julia> typeof(:(2+2))
Expr

julia> typeof(:(x))
Symbol

julia > dump(:(2+2))
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol +
    2: Int64 2
    3: Int64 2

```

From the last line is appears Symbol can be a part of expression.

Symbols are replaced with the value they are referring to when evaluated. But why we need the column name to go through parse \> evaluation? Why can’t we use a string or iris.Species to denote the column name?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 16, 2021, 5:21pm UTC](https://discourse.julialang.org/t/why-use-unevaluated-expression-to-refer-to-a-column-name/59424/4 "2021-04-16T17:21:29Z")

</div>

> [@quazirfan](#):
>
> But why we need the column name to go through parse \> evaluation?

It doesn’t. Symbols are similar to strings–they’re just frequently used as the names of things, even outside of expressions. This is mentioned in the docs linked above:

> [@rdeits](#):
>
> ```julia
> Also often used as a name or label to
> identify an entity (e.g. as a dictionary key).
> 
> ```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 16, 2021, 5:28pm UTC](https://discourse.julialang.org/t/why-use-unevaluated-expression-to-refer-to-a-column-name/59424/5 "2021-04-16T17:28:44Z")

</div>

The way I think of it is `String`s are for when it matters which characters are in them.

---

<div class="post-metadata">

**Author:** ![quazirfan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quazirfan/32/23381_2.png) [@quazirfan](https://discourse.julialang.org/u/quazirfan)\
**Post date:** [April 16, 2021, 5:42pm UTC](https://discourse.julialang.org/t/why-use-unevaluated-expression-to-refer-to-a-column-name/59424/6 "2021-04-16T17:42:06Z")

</div>

What’s the advantage of using Symbol to refer to a column name?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 16, 2021, 6:54pm UTC](https://discourse.julialang.org/t/why-use-unevaluated-expression-to-refer-to-a-column-name/59424/7 "2021-04-16T18:54:13Z")

</div>

Strings are often data, while `Symbol`s are not. So you using `Symbol`s makes it easy to know that someone is talking about the name of data.

There are also some slight performance benefits to using `Symbol`s in code in some places.

---

<div class="post-metadata">

**Author:** ![quazirfan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quazirfan/32/23381_2.png) [@quazirfan](https://discourse.julialang.org/u/quazirfan)\
**Post date:** [April 16, 2021, 7:38pm UTC](https://discourse.julialang.org/t/why-use-unevaluated-expression-to-refer-to-a-column-name/59424/8 "2021-04-16T19:38:47Z")

</div>

> [@pdeffebach](#):
>
> name of data

Isn’t name of the data is known as `variable`? I am wondering why it doesn’t use something like the following,

`coerce(iris, iris.Species => OrderedFactor)`

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 16, 2021, 8:00pm UTC](https://discourse.julialang.org/t/why-use-unevaluated-expression-to-refer-to-a-column-name/59424/9 "2021-04-16T20:00:34Z")

</div>

```julia
coerce(iris, iris.Species => OrderedFactor)

```

I’m not familiar with MLJ. But the reason this doesn’t work is because of the types and the way julia evaluate expressions.

```julia
iris.Species

```

is _just_ a vector. There is no name attached to it. It’s totally forgotten that it ever came from a data frame. so the function `coerce` is given a data frame and then a vector with no name. That’s not a lot to work with.
