# Side effects of using keyword allowduplicates in the unstack function

**URL:** <https://discourse.julialang.org/t/side-effects-of-using-keyword-allowduplicates-in-the-unstack-function/70050>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [October 19, 2021, 4:14pm UTC](https://discourse.julialang.org/t/side-effects-of-using-keyword-allowduplicates-in-the-unstack-function/70050 "2021-10-19T16:14:59Z")\
**Posts on this page:** 2\
**Page:** 1

<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:** [October 19, 2021, 4:14pm UTC](https://discourse.julialang.org/t/side-effects-of-using-keyword-allowduplicates-in-the-unstack-function/70050/1 "2021-10-19T16:14:59Z")

</div>

If I have this df

```julia
df1 = DataFrame(
           field = repeat(["A", "B", "C", "D", "E"], 12),
           data = rand(60),
           group = repeat(1:4, outer = 15))

```

if i try to do the following i get the suggestion to use allowduplicates

```julia
julia> unstack(df1,:group,:data)
ERROR: ArgumentError: Duplicate entries in unstack at row 21 for key ("A",) and variable 1. Pass allowduplicates=true to allow them.  

```

following the suggestion I get the following result.

```julia
julia> unstack(df1,:field,:data,allowduplicates=true)
4×6 DataFrame
 Row │ group A B C D E        
     │ Int64 Float64? Float64? Float64? Float64? Float64? 
─────┼─────────────────────────────────────────────────────────
   1 │ 1 0.375523 0.341876 0.360489 0.925469 0.593533
   2 │ 2 0.310565 0.21974 0.121332 0.730402 0.787124
   3 │ 3 0.37437 0.164914 0.350874 0.857969 0.291186
   4 │ 4 0.856412 0.905269 0.132294 0.168782 0.232174

```

which would be equivalent to the following code, but done explicitly.

```julia

cgl=combine(groupby(df1,[:group,:field]),:data=>last)

ucgl=unstack(cgl, :field,:data_last)

```

I am sure that the matter has been dealt with, but I have not found where and how.  
I wonder if in these cases it is not useful to add a function that does aggregation, instead of kw = allowduplicates.

---

<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:** [October 19, 2021, 4:52pm UTC](https://discourse.julialang.org/t/side-effects-of-using-keyword-allowduplicates-in-the-unstack-function/70050/2 "2021-10-19T16:52:03Z")

</div>

This could be discussed. The issue with arbitrary aggregation is that it might not have an `eltype` that matches `eltype` of the column.
