# Reduce and sum Value ( frequency matrix)

**URL:** <https://discourse.julialang.org/t/reduce-and-sum-value-frequency-matrix/46687>\
**Category:** General Usage\
**Tags:** question, dataframes\
**Created:** [September 16, 2020, 5:55am UTC](https://discourse.julialang.org/t/reduce-and-sum-value-frequency-matrix/46687 "2020-09-16T05:55:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnFlorez25](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnflorez25/32/18621_2.png) [@JohnFlorez25](https://discourse.julialang.org/u/JohnFlorez25)\
**Post date:** [September 16, 2020, 5:55am UTC](https://discourse.julialang.org/t/reduce-and-sum-value-frequency-matrix/46687/1 "2020-09-16T05:55:50Z")

</div>

Hello, I have a Dataframe with a frequency matrix generate about events in relation to a user.

 ![Captura de Pantalla 2020-09-16 a la(s) 12.48.07 a. m.](https://global.discourse-cdn.com/julialang/original/3X/8/e/8ebdcc34e5c7ce64df128bd70b663bc765dfd923.png)

I need to reduce this matrix in unique USER\_ID and sum each EVENT CATEGORY - events categories correspond to category 1,2,3,4.

I have tried using often functions in relation with DataFrames, for example, group by, filter or map but I can’t obtain a unique ID with the sum of each category values.

Thanks for the help!

---

<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:** [September 16, 2020, 6:16am UTC](https://discourse.julialang.org/t/reduce-and-sum-value-frequency-matrix/46687/2 "2020-09-16T06:16:55Z")

</div>

If I understand you correctly you just want to `groupby` the `USER_ID` and then sum the columns? You can do this:

```julia
julia> using DataFrames

julia> df = hcat(DataFrame(USER_ID = rand(100:110, 20)), DataFrame(rand(0:1, 20, 10)))
20×11 DataFrame
│ Row │ USER_ID │ x1 │ x2 │ x3 │ x4 │ x5 │ x6 │ x7 │ x8 │ x9 │ x10 │
│ │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │
├─────┼─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
│ 1 │ 107 │ 0 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │ 1 │ 0 │
│ 2 │ 100 │ 0 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 1 │
│ 3 │ 109 │ 0 │ 1 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │ 0 │ 0 │
│ 4 │ 102 │ 1 │ 1 │ 1 │ 1 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │
│ 5 │ 103 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 1 │ 1 │
│ 6 │ 109 │ 1 │ 0 │ 1 │ 0 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │
│ 7 │ 101 │ 1 │ 1 │ 0 │ 1 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │
│ 8 │ 101 │ 0 │ 0 │ 0 │ 0 │ 1 │ 1 │ 1 │ 0 │ 0 │ 0 │
│ 9 │ 102 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 0 │
│ 10 │ 106 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │ 0 │
│ 11 │ 100 │ 1 │ 1 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │ 0 │
│ 12 │ 105 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │ 1 │
│ 13 │ 101 │ 0 │ 1 │ 1 │ 1 │ 1 │ 1 │ 0 │ 0 │ 1 │ 1 │
│ 14 │ 109 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │
│ 15 │ 107 │ 0 │ 1 │ 1 │ 1 │ 1 │ 1 │ 0 │ 1 │ 1 │ 1 │
│ 16 │ 101 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │ 0 │
│ 17 │ 102 │ 0 │ 0 │ 1 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │
│ 18 │ 104 │ 0 │ 1 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │
│ 19 │ 105 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │ 1 │ 1 │ 1 │ 0 │
│ 20 │ 104 │ 1 │ 0 │ 0 │ 1 │ 1 │ 1 │ 0 │ 1 │ 0 │ 1 │

julia> to_group = names(df[!, Not(:USER_ID)])
10-element Array{String,1}:
 "x1"
 "x2"
 "x3"
 "x4"
 "x5"
 "x6"
 "x7"
 "x8"
 "x9"
 "x10"

julia> combine(groupby(df, :USER_ID), to_group .=> sum .=> to_group)
9×11 DataFrame
│ Row │ USER_ID │ x1 │ x2 │ x3 │ x4 │ x5 │ x6 │ x7 │ x8 │ x9 │ x10 │
│ │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │ Int64 │
├─────┼─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
│ 1 │ 107 │ 0 │ 1 │ 1 │ 2 │ 2 │ 2 │ 1 │ 1 │ 2 │ 1 │
│ 2 │ 100 │ 1 │ 2 │ 1 │ 0 │ 1 │ 2 │ 1 │ 1 │ 0 │ 1 │
│ 3 │ 109 │ 1 │ 1 │ 2 │ 2 │ 3 │ 3 │ 1 │ 0 │ 2 │ 0 │
│ 4 │ 102 │ 1 │ 1 │ 3 │ 2 │ 1 │ 2 │ 3 │ 3 │ 3 │ 1 │
│ 5 │ 103 │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 1 │ 1 │
│ 6 │ 101 │ 1 │ 3 │ 2 │ 3 │ 4 │ 3 │ 1 │ 1 │ 2 │ 1 │
│ 7 │ 106 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │ 0 │
│ 8 │ 105 │ 1 │ 0 │ 0 │ 0 │ 0 │ 1 │ 1 │ 1 │ 2 │ 1 │
│ 9 │ 104 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 0 │ 1 │ 0 │ 2 │

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 16, 2020, 6:17am UTC](https://discourse.julialang.org/t/reduce-and-sum-value-frequency-matrix/46687/3 "2020-09-16T06:17:09Z")

</div>

An MWE would help [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

Is this what u mean?

```julia
using DataFrames
using DataConvenience: @> 

@> df begin
  groupby(:USER_ID)
  combine( 1:12 .=> sum)
end

```

---

<div class="post-metadata">

**Author:** ![JohnFlorez25](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnflorez25/32/18621_2.png) [@JohnFlorez25](https://discourse.julialang.org/u/JohnFlorez25)\
**Post date:** [September 16, 2020, 3:35pm UTC](https://discourse.julialang.org/t/reduce-and-sum-value-frequency-matrix/46687/4 "2020-09-16T15:35:18Z")

</div>

> [@nilshg](#):
>
> `ombine(groupby(df, :USER_ID), to_group .=> sum .=> to_group)`

Thanks @nilshg , that is the solution!!
