# Replacing missings based on values in two DataFrame columns

**URL:** <https://discourse.julialang.org/t/replacing-missings-based-on-values-in-two-dataframe-columns/64764>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [July 16, 2021, 5:56pm UTC](https://discourse.julialang.org/t/replacing-missings-based-on-values-in-two-dataframe-columns/64764 "2021-07-16T17:56:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 16, 2021, 5:56pm UTC](https://discourse.julialang.org/t/replacing-missings-based-on-values-in-two-dataframe-columns/64764/1 "2021-07-16T17:56:43Z")

</div>

Given an input table like this:

```julia
 Row │ x y z       
     │ Int64 Int64 Int64?  
─────┼───────────────────────
   1 │ 100 1 missing 
   2 │ 101 1 missing 
   3 │ 102 1 missing 
   4 │ 103 1 10
   5 │ 104 2 missing 
   6 │ 105 2 missing 
   7 │ 106 2 11
   8 │ 107 2 missing 
   9 │ 108 3 12
  10 │ 109 3 missing 
  11 │ 110 3 missing 
  12 │ 111 3 missing 

```

What is the dataframian way of replacing all missings in :z by the single values also present, based on the grouping defined by :y?  
Sought result:

```julia
12×3 DataFrame
 Row │ x y z       
     │ Int64 Int64 Int64  
─────┼───────────────────────
   1 │ 100 1 10 
   2 │ 101 1 10
   3 │ 102 1 10 
   4 │ 103 1 10
   5 │ 104 2 11 
   6 │ 105 2 11 
   7 │ 106 2 11
   8 │ 107 2 11 
   9 │ 108 3 12
  10 │ 109 3 12 
  11 │ 110 3 12 
  12 │ 111 3 12 

```

Herein some code to generate input data:

```julia
using DataFrames, Random
x = 100:111
y = repeat(1:3,inner=4)
z = vcat([shuffle([i,missing,missing,missing]) for i in 10:12]...)
df = DataFrame(x=x, y=y, z=z)

```

Thanks in advance.

---

<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:** [July 16, 2021, 6:01pm UTC](https://discourse.julialang.org/t/replacing-missings-based-on-values-in-two-dataframe-columns/64764/2 "2021-07-16T18:01:27Z")

</div>

This should do it

```julia
julia> using DataFramesMeta, Random;

julia> begin
           x=100:111
           y=repeat(1:3,inner=4)
           z=vcat([shuffle([i,missing,missing,missing]) for i in 10:12]...)
           df = DataFrame(x=x, y=y, z=z)
       end
12×3 DataFrame
 Row │ x y z
     │ Int64 Int64 Int64?
─────┼───────────────────────
   1 │ 100 1 missing
   2 │ 101 1 missing
   3 │ 102 1 missing
   4 │ 103 1 10
   5 │ 104 2 missing
   6 │ 105 2 missing
   7 │ 106 2 missing
   8 │ 107 2 11
   9 │ 108 3 12
  10 │ 109 3 missing
  11 │ 110 3 missing
  12 │ 111 3 missing

julia> @chain df begin
           groupby(:y)
           @transform :z = first(skipmissing(:z))
       end
12×3 DataFrame
 Row │ x y z
     │ Int64 Int64 Int64
─────┼─────────────────────
   1 │ 100 1 10
   2 │ 101 1 10
   3 │ 102 1 10
   4 │ 103 1 10
   5 │ 104 2 11
   6 │ 105 2 11
   7 │ 106 2 11
   8 │ 107 2 11
   9 │ 108 3 12
  10 │ 109 3 12
  11 │ 110 3 12
  12 │ 111 3 12

```
