# @replacena not working with variables as replacement value

**URL:** https://discourse.julialang.org/t/replacena-not-working-with-variables-as-replacement-value/45401
**Category:** New to Julia
**Tags:** query
**Created:** [August 23, 2020, 4:09am UTC](https://discourse.julialang.org/t/replacena-not-working-with-variables-as-replacement-value/45401 "2020-08-23T04:09:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![TheRoniOne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theronione/32/17359_2.png) [@TheRoniOne](https://discourse.julialang.org/u/TheRoniOne)
#### Post date: [August 23, 2020, 4:09am UTC](https://discourse.julialang.org/t/replacena-not-working-with-variables-as-replacement-value/45401/1 "2020-08-23T04:09:59Z")

</div>

Hi, was trying to practice some simple data cleaning with Query.jl modifying a bit an example of the documentation.

What I intended to do was to replace all the missing values in a column with the mean of that column.

> using Queryverse  
> using Statistics
> 
> df = DataFrame(a=[1,2,missing], b=[“One”,missing,“Three”])  
> myMean = mean(skipmissing(df.a))  
> q = df |\> @replacena(:b=\>“Unknown”, :a=\>myMean) |\> DataFrame

Wich trigered an error.

> julia\> q = df |\> @replacena(:b=\>“Unknown”, :a=\>myMean) |\> DataFrame  
> ERROR: UndefVarError: myMean not defined

So I decided to try using another function of Query to see if it was expected to work that way.

> julia\> df |\> @mutate(c = myMean)  
> 3x3 query result  
> a │ b │ c  
> ────┼─────────┼────  
> 1 │ “One” │ 1.5  
> 2 │ #NA │ 1.5  
> #NA │ “Three” │ 1.5

And it did.

Finally, decided to try without using a variable and just using the function that returns the value wich I was trying to use to replace missing values with.

> q = df |\> @replacena(:b=\>“Unknown”, :a=\>mean(skipmissing(\_.a))) |\> DataFrame

Still got an error.

> julia\> q = df |\> @replacena(:b=\>“Unknown”, :a=\>mean(skipmissing(\_.a))) |\> DataFrame  
> ERROR: UndefVarError: mean not defined

Any help will be apreciated.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 23, 2020, 9:19am UTC](https://discourse.julialang.org/t/replacena-not-working-with-variables-as-replacement-value/45401/2 "2020-08-23T09:19:14Z")

</div>

I can only guess, despite your fine MWE but Queryverse doesn’t compile in my environment (python36.dll missing).  
The docs  
[https://www.queryverse.org/Query.jl/stable/standalonequerycommands/#The-@replacena-command-1](https://www.queryverse.org/Query.jl/stable/standalonequerycommands/#The-@replacena-command-1)  
aren’t explicit but talk only about values. So my guess is that  
`q = df |> @replacena(:b=>"Unknown", :a=> $myMean ) |> DataFrame`  
could do.  
[https://docs.julialang.org/en/v1/manual/metaprogramming/#man-expression-interpolation-1](https://docs.julialang.org/en/v1/manual/metaprogramming/#man-expression-interpolation-1)

---

<div class="post-metadata">

### Author: ![TheRoniOne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theronione/32/17359_2.png) [@TheRoniOne](https://discourse.julialang.org/u/TheRoniOne)
#### Post date: [August 23, 2020, 4:43pm UTC](https://discourse.julialang.org/t/replacena-not-working-with-variables-as-replacement-value/45401/3 "2020-08-23T16:43:40Z")

</div>

Tried with  
`julia> q = df |> @replacena(:b=>"Unknown", :a=> $myMean ) |> DataFrame`

Got  
`ERROR: syntax: "$" expression outside quote around C:\Users\RoniD\.julia\packages\Query\85Sw7\src\query_translation.jl:58`

Also tried with  
`julia> q = df |> @replacena(:b=>"Unknown", :a=> "$myMean") |> DataFrame`

And got another error  
`ERROR: UndefVarError: myMean not defined`

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 23, 2020, 5:09pm UTC](https://discourse.julialang.org/t/replacena-not-working-with-variables-as-replacement-value/45401/4 "2020-08-23T17:09:10Z")

</div>

Ok, I had to remove my .julia to get a working Queryverse#master. This is the working syntax:

```julia
julia> using DataFrames, Queryverse, Statistics

julia> df = DataFrame(a=[1,2,missing], b=["One",missing,"Three"])
3×2 DataFrame
│ Row │ a │ b │
│ │ Int64? │ String? │
├─────┼─────────┼─────────┤
│ 1 │ 1 │ One │
│ 2 │ 2 │ missing │
│ 3 │ missing │ Three │

julia> myMean = mean(skipmissing(df.a))
1.5

julia> q = df |> @replacena(:b=>"Unknown", :a => :( $myMean ) ) |> DataFrame
3×2 DataFrame
│ Row │ a │ b │
│ │ Any │ Any │
├─────┼─────┼─────────┤
│ 1 │ 1 │ One │
│ 2 │ 2 │ Unknown │
│ 3 │ 1.5 │ Three │

```

---

<div class="post-metadata">

### Author: ![TheRoniOne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theronione/32/17359_2.png) [@TheRoniOne](https://discourse.julialang.org/u/TheRoniOne)
#### Post date: [August 23, 2020, 5:18pm UTC](https://discourse.julialang.org/t/replacena-not-working-with-variables-as-replacement-value/45401/5 "2020-08-23T17:18:25Z")

</div>

Thanks!  
It does work now.
