# How do I add a new column to a dataframe in Julia using conditional logic?

**URL:** <https://discourse.julialang.org/t/how-do-i-add-a-new-column-to-a-dataframe-in-julia-using-conditional-logic/77103>\
**Category:** General Usage\
**Tags:** dataframes, dataframesmeta\
**Created:** [February 25, 2022, 11:51pm UTC](https://discourse.julialang.org/t/how-do-i-add-a-new-column-to-a-dataframe-in-julia-using-conditional-logic/77103 "2022-02-25T23:51:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![a.frist](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@a.frist](https://discourse.julialang.org/u/a.frist)\
**Post date:** [February 25, 2022, 11:51pm UTC](https://discourse.julialang.org/t/how-do-i-add-a-new-column-to-a-dataframe-in-julia-using-conditional-logic/77103/1 "2022-02-25T23:51:26Z")

</div>

I want to create a new column in my dataframe. If another column equals zero, I want this column to equal zero. If it doesn’t, I would like this column to equal another column divided by a third column. I tried using the @rtransform dataframes meta macro but keep getting various errors. Is there a generally accepted way to do this?

---

<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:** [February 26, 2022, 12:21am UTC](https://discourse.julialang.org/t/how-do-i-add-a-new-column-to-a-dataframe-in-julia-using-conditional-logic/77103/2 "2022-02-26T00:21:12Z")

</div>

```julia
@rtransform df :z = :y == 0 ? 0 : :z

```

---

<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:** [February 26, 2022, 12:39am UTC](https://discourse.julialang.org/t/how-do-i-add-a-new-column-to-a-dataframe-in-julia-using-conditional-logic/77103/3 "2022-02-26T00:39:43Z")

</div>

It’s true that it might be nice for a more convenient `@replace` command because there is no way to do this without repeating the variable name more frequently than is ideal.

---

<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:** [February 26, 2022, 9:22am UTC](https://discourse.julialang.org/t/how-do-i-add-a-new-column-to-a-dataframe-in-julia-using-conditional-logic/77103/4 "2022-02-26T09:22:31Z")

</div>

Base Julia:

```julia
julia> df = DataFrame(a = Int.(rand(Bool, 10)), b = 1:10, c = 10:-1:1)

julia> df.d = @. ifelse(df.a == 0, 0.0, df.b / df.c); df
10×4 DataFrame
 Row │ a b c d        
     │ Int64 Int64 Int64 Float64  
─────┼───────────────────────────────
   1 │ 0 1 10 0.0
   2 │ 0 2 9 0.0
   3 │ 1 3 8 0.375
   4 │ 0 4 7 0.0
   5 │ 0 5 6 0.0
   6 │ 0 6 5 0.0
   7 │ 1 7 4 1.75
   8 │ 1 8 3 2.66667
   9 │ 1 9 2 4.5
  10 │ 1 10 1 10.0

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 26, 2022, 11:39am UTC](https://discourse.julialang.org/t/how-do-i-add-a-new-column-to-a-dataframe-in-julia-using-conditional-logic/77103/5 "2022-02-26T11:39:23Z")

</div>

You could calculate/construct the new column, by putting into formula the conditions you have illustrated

```julia
df.d.=(df.b./df.c).*(df.a.!=0)
#or 
@. df.d=(df.b/df.c)*(df.a!=0)

```

---

<div class="post-metadata">

**Author:** ![a.frist](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@a.frist](https://discourse.julialang.org/u/a.frist)\
**Post date:** [February 26, 2022, 2:57pm UTC](https://discourse.julialang.org/t/how-do-i-add-a-new-column-to-a-dataframe-in-julia-using-conditional-logic/77103/6 "2022-02-26T14:57:03Z")

</div>

This worked perfectly. Thank you @pdeffebach !
