# Change values to missing in a column

**URL:** <https://discourse.julialang.org/t/change-values-to-missing-in-a-column/82858>\
**Category:** General Usage\
**Created:** [June 16, 2022, 7:05am UTC](https://discourse.julialang.org/t/change-values-to-missing-in-a-column/82858 "2022-06-16T07:05:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Billpete002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/billpete002/32/35091_2.png) [@Billpete002](https://discourse.julialang.org/u/Billpete002)\
**Post date:** [June 16, 2022, 7:05am UTC](https://discourse.julialang.org/t/change-values-to-missing-in-a-column/82858/1 "2022-06-16T07:05:24Z")

</div>

So I have been trying to wrap my head around how to set a value to missing with an IF ELSE statement… It seems like this should be an easy way to do it:

```julia
df= @chain df begin
@transform :x= begin 
        if :x != 0
            :x
            else missing
        end
    end
end

```

but the results are unchanged:

```julia

168779-element Vector{Int64}:
 20220810
 20220810
 20220810
 20220810
 20220810
 20220810
 20220810
 20220810
 20220810
 20220810
 20220810
 20220810
 20220810
        ⋮
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
        0
 20220705

```

Ultimately I need those zeros to be missing so I can run:

`df.x= Dates.Date.(string.(df.x), dateformat"yyyymmdd")`

but I can’t with value 0 - however with a missing I can just add a passmissing(x) in addition to the string()

OR is there an easier way altogether that I just haven’t seen yet?

---

<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:** [June 16, 2022, 7:20am UTC](https://discourse.julialang.org/t/change-values-to-missing-in-a-column/82858/2 "2022-06-16T07:20:17Z")

</div>

Why do you need all those machinery, could you not just do

```julia
ifelse.(df.x .== 0, missing, Date.(string.(df.x), dateformat"yyyymmdd"))

```

(btw if performance matters you can probably do better by using `digits` of your Int to directly construct the date rather than allocating a string and then parsing that)

---

<div class="post-metadata">

**Author:** ![Billpete002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/billpete002/32/35091_2.png) [@Billpete002](https://discourse.julialang.org/u/Billpete002)\
**Post date:** [June 16, 2022, 7:29am UTC](https://discourse.julialang.org/t/change-values-to-missing-in-a-column/82858/3 "2022-06-16T07:29:41Z")

</div>

hmm this gave me:

> LoadError: ArgumentError: Unable to parse date time. Expected directive DatePart(yyyy) at char 1

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [June 16, 2022, 10:04am UTC](https://discourse.julialang.org/t/change-values-to-missing-in-a-column/82858/4 "2022-06-16T10:04:47Z")

</div>

> [@Billpete002](#):
>
> ```julia
> df= @chain df begin
> @transform :x= begin 
> if :x != 0
> :x
> else missing
> end
> end
> end
> 
> ```

You should use macro `@byrow`,

```julia
julia> df= @chain df begin
       @transform @byrow :x= begin
               if :x != 0
                   :x
                   else missing
               end
           end
       end

```

The easyway to do this is

```julia
@transform df @byrow :x = ifelse(:x == 0, missing, :x)

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [June 16, 2022, 10:41am UTC](https://discourse.julialang.org/t/change-values-to-missing-in-a-column/82858/5 "2022-06-16T10:41:41Z")

</div>

or with `@rtransform` the `@byrow` part is not needed.

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [June 16, 2022, 10:49am UTC](https://discourse.julialang.org/t/change-values-to-missing-in-a-column/82858/6 "2022-06-16T10:49:04Z")

</div>

Great. Thanks.

```julia
@rtransform! df :x = ifelse(:a == 0, missing, :x)

```

---

<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:** [June 16, 2022, 11:59am UTC](https://discourse.julialang.org/t/change-values-to-missing-in-a-column/82858/7 "2022-06-16T11:59:29Z")

</div>

Do you mean something like this or is there some function that does the transformation directly?

```julia

df.date= datefromint.(df.x)

function datefromint(d::Int)
    if d!=0
    Date(div(d,10^4),div(rem(d,10^4),10^2),rem(d,10^2))
    else
        missing
    end
end

```

But the most important reason why I intervened is to ask for clarification on the functioning of the ifelse function

```julia

df.date= map(d->ifelse(d!=0, datefromint(d),missing), df.x)

datefromint(d::Int)=Date(div(d,10^4),div(rem(d,10^4),10^2),rem(d,10^2))

```

It seems that ifelse evaluates the datefromint function even if d == 0.  
I wonder if this is the case in general, that is, if ifelse evaluates both the “then” and “else” conditions before the return?  
But it is more likely that there is something (or more than something) that he did not understand.

---

<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:** [June 16, 2022, 1:42pm UTC](https://discourse.julialang.org/t/change-values-to-missing-in-a-column/82858/8 "2022-06-16T13:42:45Z")

</div>

You can still use the ternary notation.

```julia
@rtransform! df :x = :a == 0 ? missing : :x

```

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [June 16, 2022, 9:35pm UTC](https://discourse.julialang.org/t/change-values-to-missing-in-a-column/82858/9 "2022-06-16T21:35:15Z")

</div>

Thanks, it should be

```julia
@rtransform! df :x = :a == 0 ? missing : :x

```

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [June 24, 2022, 9:12am UTC](https://discourse.julialang.org/t/change-values-to-missing-in-a-column/82858/10 "2022-06-24T09:12:16Z")

</div>

You can also use `replace!` just like Stata. I like this way.

```julia
replace!(df.x, 0=>missing)

```
