# Coalesce function not working with data frames n x n with n\>=2

**URL:** <https://discourse.julialang.org/t/coalesce-function-not-working-with-data-frames-n-x-n-with-n-2/20624>\
**Category:** Internals & Design\
**Created:** [February 10, 2019, 3:49pm UTC](https://discourse.julialang.org/t/coalesce-function-not-working-with-data-frames-n-x-n-with-n-2/20624 "2019-02-10T15:49:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erisme](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@Erisme](https://discourse.julialang.org/u/Erisme)\
**Post date:** [February 10, 2019, 3:49pm UTC](https://discourse.julialang.org/t/coalesce-function-not-working-with-data-frames-n-x-n-with-n-2/20624/1 "2019-02-10T15:49:38Z")

</div>

Hi,

y=dataframe 6x6

I may be wrong but I tried to use coalesce function on a dataframe 6x6 and I get the error: no method matching iterate.  
coalesce.(y,0) ==\> Error  
But if I try coalesce.(y[i],0) for each i it works.  
any help?  
Thank you

---

<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 10, 2019, 4:29pm UTC](https://discourse.julialang.org/t/coalesce-function-not-working-with-data-frames-n-x-n-with-n-2/20624/2 "2019-02-10T16:29:44Z")

</div>

It might help if you could read [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757), and then post a MWE - it is currently unclear what you are trying to do.

From the REPL:

```julia
help?> coalesce
search: coalesce

  coalesce(x, y...)

  Return the first value in the arguments which is not equal to missing, if
  any. Otherwise return missing.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> coalesce(missing, 1)
  1

  julia> coalesce(1, missing)
  1

  julia> coalesce(nothing, 1) # returns `nothing`

  julia> coalesce(missing, missing)
  missing

```

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [February 10, 2019, 5:39pm UTC](https://discourse.julialang.org/t/coalesce-function-not-working-with-data-frames-n-x-n-with-n-2/20624/3 "2019-02-10T17:39:25Z")

</div>

Broadcasting on a dataframe doesn’t go through every cell, it goes though each row. Non of the rows are missing, so coalesce didn’t do anything. Also, it’s not mutating. Not sure if there’s a better way, but I often do

```julia
for n in names (df)
    df[n] = coalesce.(df[n], 0)
end

```

---

<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 10, 2019, 5:48pm UTC](https://discourse.julialang.org/t/coalesce-function-not-working-with-data-frames-n-x-n-with-n-2/20624/4 "2019-02-10T17:48:03Z")

</div>

Could you clarify what operation you are trying to perform? Are you looking to replace each `missing` in a DataFrame with 0?

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 10, 2019, 9:03pm UTC](https://discourse.julialang.org/t/coalesce-function-not-working-with-data-frames-n-x-n-with-n-2/20624/5 "2019-02-10T21:03:11Z")

</div>

It’s been evoked several times to add an argument to `disallowmissing!` to do that. Should be easy to do.

---

<div class="post-metadata">

**Author:** ![Erisme](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@Erisme](https://discourse.julialang.org/u/Erisme)\
**Post date:** [February 11, 2019, 3:18am UTC](https://discourse.julialang.org/t/coalesce-function-not-working-with-data-frames-n-x-n-with-n-2/20624/6 "2019-02-11T03:18:29Z")

</div>

Sorry if I wasn’t enough clear.  
I have a multi-dimension dataframe y (size is 6x6 see picture)

 ![y%20dataframe](https://global.discourse-cdn.com/julialang/original/3X/c/2/c2378bc2af9ff4aa36c5f92aa029463fa43f5a12.png)  
To replace missing value by 0 in this data frame, I tried: coalesce.(y, 0) but I got an error message. The only way to do this is : [y[i] = coalesce.(y[i], 0) for i in 1:size(y,2)]  
I would have thought that coalesce.(y, 0) will replace any missing value in the DataFrame which is not working, it only works for DataFrame vector but not DataFrame nXn  
by doing : [y[i] = coalesce.(y[i], 0) for i in 1:size(y,2)]  
I get the 0 instead of missings

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [February 11, 2019, 6:26pm UTC](https://discourse.julialang.org/t/coalesce-function-not-working-with-data-frames-n-x-n-with-n-2/20624/7 "2019-02-11T18:26:17Z")

</div>

Did you try what I suggested?

> [@kevbonham](#):
>
> Not sure if there’s a better way, but I often do
> 
> ```julia
> for n in names (df)
> df[n] = coalesce.(df[n], 0)
> end
> 
> ```

?

Your way works, but I think also allocates a vector. As I said, broadcasting over a dataframe applies the function to rows, not cells.

---

<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:** [February 11, 2019, 9:07pm UTC](https://discourse.julialang.org/t/coalesce-function-not-working-with-data-frames-n-x-n-with-n-2/20624/8 "2019-02-11T21:07:18Z")

</div>

Even simpler and more efficient (note this is an in-place operation, but I understand this is what you want) is:

```julia
replace!.(eachcol(df, false), Ref(missing=>0))

```

And `eachcol(df1, false)` will be simply replaced by `eachcol(df1)` in the near future when the deprecation period for the old way that `eachcol` worked finishes.

---

<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:** [February 11, 2019, 9:21pm UTC](https://discourse.julialang.org/t/coalesce-function-not-working-with-data-frames-n-x-n-with-n-2/20624/9 "2019-02-11T21:21:14Z")

</div>

> [@kevbonham](#):
>
> Broadcasting on a dataframe doesn’t go through every cell, it goes though each row.

Actually currently AFAIK you cannot broadcast an `AbstractDataFrame` now. I am not clear if it will be supported in the future, but if it will then it will be most probably row-wise as @kevbonham indicates.

For now you have `eachcol` and `eachrow` methods that you can call and they support broadcasting col-wise and row-wise respectively.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [February 12, 2019, 8:18pm UTC](https://discourse.julialang.org/t/coalesce-function-not-working-with-data-frames-n-x-n-with-n-2/20624/10 "2019-02-12T20:18:58Z")

</div>

Ah, this is a good solution. I keep forgetting about `Ref()` for stuff like this.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [February 12, 2019, 8:21pm UTC](https://discourse.julialang.org/t/coalesce-function-not-working-with-data-frames-n-x-n-with-n-2/20624/11 "2019-02-12T20:21:15Z")

</div>

Note that doing an in-place operation for this isn’t necessarily a good idea since it will keep the `Union{T,Missing}` element type even if there are no missing values, which can hurt performance and doesn’t indicate that missing values are not supposed to be present.
