# How to do a reduceByKey in Julia

**URL:** <https://discourse.julialang.org/t/how-to-do-a-reducebykey-in-julia/20369>\
**Category:** New to Julia\
**Created:** [February 1, 2019, 10:32pm UTC](https://discourse.julialang.org/t/how-to-do-a-reducebykey-in-julia/20369 "2019-02-01T22:32:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DWSchulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dwschulze/32/35968_2.png) [@DWSchulze](https://discourse.julialang.org/u/DWSchulze)\
**Post date:** [February 1, 2019, 10:32pm UTC](https://discourse.julialang.org/t/how-to-do-a-reducebykey-in-julia/20369/1 "2019-02-01T22:32:24Z")

</div>

I have an Array{String,1} and I need to get a count of each String in the Array. In Spark this is done with reduceByKey. What’s the Julia equivalent?

I’ve seen approaches using DataFrames, but I can’t convert the Array{String,1} into a DataFrame if that is the right approach.

So really two questions: How do I do reduceByKey in Julia and can I convert Array{String,1} to a DataFrame?

---

<div class="post-metadata">

**Author:** ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)\
**Post date:** [February 1, 2019, 10:44pm UTC](https://discourse.julialang.org/t/how-to-do-a-reducebykey-in-julia/20369/2 "2019-02-01T22:44:58Z")

</div>

The `StatsBase` package has what you are probably looking for.

```julia
using StatsBase

a=rand('a':'f',50)

countmap(a)
Dict{Char,Int64} with 6 entries:
  'f' => 11
  'd' => 8
  'c' => 9
  'e' => 10
  'a' => 6
  'b' => 6

```

---

<div class="post-metadata">

**Author:** ![DWSchulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dwschulze/32/35968_2.png) [@DWSchulze](https://discourse.julialang.org/u/DWSchulze)\
**Post date:** [February 2, 2019, 12:02am UTC](https://discourse.julialang.org/t/how-to-do-a-reducebykey-in-julia/20369/3 "2019-02-02T00:02:41Z")

</div>

That worked, thanks.

How would I plot a dictionary with keys on the x-axis and values on the y-axis?

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [February 2, 2019, 2:38am UTC](https://discourse.julialang.org/t/how-to-do-a-reducebykey-in-julia/20369/4 "2019-02-02T02:38:41Z")

</div>

> [@Daniel\_Berge](#):
>
> countmap(a)

Using Plots.jl:

```julia
using StatsBase, # for the countmap function
      Plots # to plot
gr() # setup plotting backend, GR is default.  
# There is also PyPlot, PlotlyJS, and PGFPlots.

a=rand('a':'f',50) # create a random distribution of letters a through f to serve as the dataset

dc = countmap(a) # convert dataset to a Dict

bar(
  string.(collect(keys(dc))), # the x-axis - the keys of the dictionary collected into an Array converted to Strings
  collect(values(dc)), # the y-axis - frequency.
  xlabel = "Letter", # the label on the x-axis
  ylabel = "Frequency",# the label on the y-axis
  title = "Frequency per letter"
) 

```

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 2, 2019, 7:42am UTC](https://discourse.julialang.org/t/how-to-do-a-reducebykey-in-julia/20369/5 "2019-02-02T07:42:04Z")

</div>

I discussed various ways to do it here:

[How to count all unique character frequency in a string?](https://discourse.julialang.org/t/how-to-count-all-unique-character-frequency-in-a-string/19342/16)

`countmap` is an excellent choice, if you don’t mind the dependency on `StatsBase`.

---

<div class="post-metadata">

**Author:** ![a5vzener](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/a5vzener/32/16257_2.png) [@a5vzener](https://discourse.julialang.org/u/a5vzener)\
**Post date:** [February 4, 2019, 6:11pm UTC](https://discourse.julialang.org/t/how-to-do-a-reducebykey-in-julia/20369/6 "2019-02-04T18:11:46Z")

</div>

I like Gadfly for plotting. It’s little easier with DataFrames, but arrays work as well.

> using Gadfly  
> using StatsBase  
> a=rand(‘a’:‘f’,100)  
> m=countmap(a)  
> d=hcat(collect(keys(m)),collect(values(m))) # create a 2x100 array with the results of countmap  
> d=sortslices(d,dims=1,by=x-\>x[1]) # sort the whole array by character in column 1  
> plot(d,x=convert.(Int,d[:,1]),y=d[:,2],Scale.x\_discrete(labels=x-\>Char(x)),Geom.bar(position=:dodge), Guide.xlabel(“Character”),Guide.ylabel(“Count”),Theme(bar\_spacing=1cm))

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [February 6, 2019, 1:40am UTC](https://discourse.julialang.org/t/how-to-do-a-reducebykey-in-julia/20369/7 "2019-02-06T01:40:37Z")

</div>

Here is the [Queryverse](https://github.com/queryverse) way of doing it:

```julia
rand('a':'f',100) |> 
  @groupby(_) |>
  @map({char=key(_), count=length(_)}) |>
  @vlplot(:bar, x="count", y="char:n")

```

It is not entirely clear though whether this starts with the kind of data structure you were asking for? Here we start with a `Vector{Char}`, not a `Vector{String}`, which you were asking about?
