# Convert symbol to expression

**URL:** https://discourse.julialang.org/t/convert-symbol-to-expression/74016
**Category:** General Usage
**Tags:** metaprogramming, dataframes
**Created:** [January 4, 2022, 6:04am UTC](https://discourse.julialang.org/t/convert-symbol-to-expression/74016 "2022-01-04T06:04:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [January 4, 2022, 6:04am UTC](https://discourse.julialang.org/t/convert-symbol-to-expression/74016/1 "2022-01-04T06:04:55Z")

</div>

I’m trying to complete a DataFrame that omits some years with the average of adjacent years. For example:

```julia
using DataFrames

df = DataFrame(year = [1995, 1996, 1997, 1999, 2001], x = float(1:5))

```

From that, I am trying to obtain:

```julia
DataFrame(year = 1995:2001, x = [1, 2, 3, 3.5, 4, 4.5, 5])

```

This is what I’ve done:

```julia
using StatsBase

function tsfill!(df, t=:year, xfill=:x)
    maxy = maximum(df.t)
    miny = minimum(df.t)
    completeyears = miny:maxy
    yearstofill = filter(x -> x ∉ df.t, completeyears)
    for y in yearstofill
        sub = subset(df, t => yr -> yr .== y - 1 .|| yr .== y + 1)
        append!(df, DataFrame(t = y, xfill = mean(sub[:, xfill])))
    end
    sort!(df, t)
    return df
end

```

That function errors in the expression `DataFrame(t = y, xfill = mean(sub[:, xfill]))` since `t` and `xfill` are `Symbol`s and `DataFrame` expects a normal expression (ie `year = y` and `x = mean(...)`. How can I convert from `Symbol` to “normal expression”?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [January 4, 2022, 6:30am UTC](https://discourse.julialang.org/t/convert-symbol-to-expression/74016/2 "2022-01-04T06:30:25Z")

</div>

You don’t have to, just do `DataFrame(t => y)` - the constructor called with a pair accepts a string/symbol on the left hand side of the pair.

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [January 4, 2022, 7:46am UTC](https://discourse.julialang.org/t/convert-symbol-to-expression/74016/3 "2022-01-04T07:46:04Z")

</div>

Is there a package for doing these kind of “administrative tasks” for DataFrames?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [January 4, 2022, 8:56am UTC](https://discourse.julialang.org/t/convert-symbol-to-expression/74016/4 "2022-01-04T08:56:53Z")

</div>

Not that I’m aware of, although I’m not entirely sure what your definition of “administrative task” is - what you’re doing here is a specific imputation scheme, for which there are packages like Impute.jl, which has `substitute`

[https://invenia.github.io/Impute.jl/stable/api/imputation/#Impute.substitute](https://invenia.github.io/Impute.jl/stable/api/imputation/#Impute.substitute)

as well as a k-nearest neighbour imputation scheme - either of those might be amenable to what you’re doing here although I haven’t tried.

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [January 5, 2022, 4:30am UTC](https://discourse.julialang.org/t/convert-symbol-to-expression/74016/5 "2022-01-05T04:30:32Z")

</div>

I mean something like data management for DataFrames. I find the tools provided in the default package to be too low level, so I usually do any data management in Stata before bringing it to Julia.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [January 5, 2022, 6:52am UTC](https://discourse.julialang.org/t/convert-symbol-to-expression/74016/6 "2022-01-05T06:52:15Z")

</div>

Again very hard to say without a better idea of what you mean by \*data management" - I’d say there’s very little that you can do in Stata that you can’t do in DataFrames (mainly manipulations of panel data which leverage Stata’s ability to set an id and time dimension, although I haven’t used Stata seriously in about five years), while at the same time there’s lots of stuff you can do on dataframes that you would struggle to do in stata (without resorting to using Mata), simply because you have all the power and expressivity of base Julia at your disposal.

If it’s just about dataframes being more verbose than stata (because you have to write `df[df.col1 .> 1, :] ` etc you might want to look into DataFramesMeta.

Other than that I don’t think where are packages for what I world consider “data management” (ie filtering, transforming, aggregating data) of DataFrames as DataFrames **is** the package designed for this already.

So the most useful thing from your perspective is probably to ask here for solutions to specific “data management” tasks which you think can’t be done in DataFrames.jl
