# DataFrames, aggregate with missings

**URL:** https://discourse.julialang.org/t/dataframes-aggregate-with-missings/38737
**Category:** Data
**Tags:** dataframes
**Created:** [May 4, 2020, 1:13pm UTC](https://discourse.julialang.org/t/dataframes-aggregate-with-missings/38737 "2020-05-04T13:13:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![donquicote](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@donquicote](https://discourse.julialang.org/u/donquicote)
#### Post date: [May 4, 2020, 1:13pm UTC](https://discourse.julialang.org/t/dataframes-aggregate-with-missings/38737/1 "2020-05-04T13:13:45Z")

</div>

Hi!

I’m facing the following problem:

```julia
using DataFrames, Statistics

df = DataFrame(A = [1, 2, missing, missing, missing, 3, 4, 5],
                B = [1, 1, 2, 2, 3, 3, 4, 4])

df_mean = aggregate(df, :B, x -> mean(skipmissing(x)))

```

The mean function returns `NaN` when using `skipmissing` and all the observations in that group are `missing`. Is there a way to change this behaviour so that it returns `missing` as well?

Thank you!

---

<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: [May 4, 2020, 3:01pm UTC](https://discourse.julialang.org/t/dataframes-aggregate-with-missings/38737/2 "2020-05-04T15:01:16Z")

</div>

The reasoning for this behavior is that a skipmissing of a vector `Int[missing, missing]` should be have kind of the same behavior as an empty `Int` vector, `Int[]`. Mean is just `mean(x) = sum(x) / length(x)` so it’s clear that `mean(Int[])` should return NaN.

Are you coming from Stata, by chance? Julia’s behavior mimics R’s, but Stata propagates `missing` they way you expect it to.

The best approach would be to make a little helper function

```julia
meanmissing(x) = all(ismissing, x) ? missing : mean(skipmissing(x))

```

---

<div class="post-metadata">

### Author: ![donquicote](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@donquicote](https://discourse.julialang.org/u/donquicote)
#### Post date: [May 4, 2020, 3:25pm UTC](https://discourse.julialang.org/t/dataframes-aggregate-with-missings/38737/3 "2020-05-04T15:25:45Z")

</div>

> [@pdeffebach](#):
>
> meanmissing(x) = all(ismissing, x) ? missing : mean(skipmissing(x))

This is exactly what I was looking for! Thanks a lot. I’m coming from Stata and Pandas, which in this case seem to behave alike.
