# Does Query.jl have a good way to implement mutations that depend on each other?

**URL:** <https://discourse.julialang.org/t/does-query-jl-have-a-good-way-to-implement-mutations-that-depend-on-each-other/44643>\
**Category:** Data\
**Created:** [August 9, 2020, 9:25pm UTC](https://discourse.julialang.org/t/does-query-jl-have-a-good-way-to-implement-mutations-that-depend-on-each-other/44643 "2020-08-09T21:25:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilyagr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ilyagr/32/17022_2.png) [@ilyagr](https://discourse.julialang.org/u/ilyagr)\
**Post date:** [August 9, 2020, 9:25pm UTC](https://discourse.julialang.org/t/does-query-jl-have-a-good-way-to-implement-mutations-that-depend-on-each-other/44643/1 "2020-08-09T21:25:09Z")

</div>

The following doesn’t work because latter definitions can’t depend on previous ones:

```julia
DataFrame(bad=[1,2], good=[0, 3]) |> @map((
			count = _.bad + _.good,
			bad_rate = _.bad/_.count,
			bad_pct =round(_.bad_rate*100;digits=1)
		))

```

Is there any convenient way to do such things? I thought `@mutate` might work, but [it doesn’t](https://github.com/queryverse/Query.jl/issues/314).

---

<div class="post-metadata">

**Author:** ![ilyagr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ilyagr/32/17022_2.png) [@ilyagr](https://discourse.julialang.org/u/ilyagr)\
**Post date:** [August 10, 2020, 1:00am UTC](https://discourse.julialang.org/t/does-query-jl-have-a-good-way-to-implement-mutations-that-depend-on-each-other/44643/2 "2020-08-10T01:00:51Z")

</div>

I think I’ve found a way to do this using the [LINQ `@let` syntax](https://www.queryverse.org/Query.jl/v1.0/linqquerycommands/#Range-variables-1). It’s a little longer, but it seems pretty clear to me.

```julia
	df = DataFrame(bad=[1,2], good=[0, 3]) 
	
	@from i in df begin
		@let count = i.bad + i.good
		@let bad_rate = i.bad/count
		@let bad_pct =round(bad_rate*100;digits=1)
		@select {i.good, i.bad, count, bad_rate, bad_pct}
		@collect DataFrame # Optional
	end

```

The two styles can even be combined if necessary, with only a little contortion:

```julia
DataFrame(bad=[1,2], good=[0, 3]) |> df -> (
	@from i in df begin
		@let count = i.bad + i.good
		@let bad_rate = i.bad/count
		@let bad_pct =round(bad_rate*100;digits=1)
		@where bad_rate > 0.7
		@select {i.good, i.bad, count, bad_rate, bad_pct}
	end) |> save("somewhere.csv")

```

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [August 10, 2020, 9:35pm UTC](https://discourse.julialang.org/t/does-query-jl-have-a-good-way-to-implement-mutations-that-depend-on-each-other/44643/3 "2020-08-10T21:35:57Z")

</div>

I would write this as

```julia
DataFrame(bad=[1,2], good=[0, 3]) |>
@mutate(count = _.bad + _.good) |>
@mutate(bad_rate = _.bad/_.count) |>
@mutate(bad_pct =round(_.bad_rate*100;digits=1))

```

---

<div class="post-metadata">

**Author:** ![ilyagr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ilyagr/32/17022_2.png) [@ilyagr](https://discourse.julialang.org/u/ilyagr)\
**Post date:** [August 11, 2020, 8:26pm UTC](https://discourse.julialang.org/t/does-query-jl-have-a-good-way-to-implement-mutations-that-depend-on-each-other/44643/4 "2020-08-11T20:26:16Z")

</div>

That works. Thanks!
