# Is there any smart way of doing pivot table?

**URL:** https://discourse.julialang.org/t/is-there-any-smart-way-of-doing-pivot-table/15636
**Category:** General Usage
**Tags:** question
**Created:** [September 28, 2018, 7:02pm UTC](https://discourse.julialang.org/t/is-there-any-smart-way-of-doing-pivot-table/15636 "2018-09-28T19:02:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![youngjae.woo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youngjae.woo/32/4750_2.png) [@youngjae.woo](https://discourse.julialang.org/u/youngjae.woo)
#### Post date: [September 28, 2018, 7:02pm UTC](https://discourse.julialang.org/t/is-there-any-smart-way-of-doing-pivot-table/15636/1 "2018-09-28T19:02:07Z")

</div>

I often look at pivot table (one like R function’s `table`) a lot for single dim array. It’s not too difficult to do so and am sharing my usage so far. However, I’m sure there are better and more intuitive way of doing this. Any ideas?

```julia
using StatsBase, DataFrames
function make_pivottable(datvec)
    cmap = countmap(datvec)
    k = string.(collect(keys(cmap)))
    v = collect(values(cmap))
    ptab = DataFrame([k v], [:key, :counts])
    sort!(ptab, :counts, rev=true)

    return ptab
end

make_pivottable(somevector)

```

---

<div class="post-metadata">

### Author: ![ValdarT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/valdart/32/24146_2.png) [@ValdarT](https://discourse.julialang.org/u/ValdarT)
#### Post date: [September 28, 2018, 7:55pm UTC](https://discourse.julialang.org/t/is-there-any-smart-way-of-doing-pivot-table/15636/2 "2018-09-28T19:55:08Z")

</div>

[https://github.com/nalimilan/FreqTables.jl](https://github.com/nalimilan/FreqTables.jl)

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [March 27, 2020, 5:58pm UTC](https://discourse.julialang.org/t/is-there-any-smart-way-of-doing-pivot-table/15636/3 "2020-03-27T17:58:47Z")

</div>

> [@youngjae.woo](#):
>
> function make\_pivottable(datvec) cmap = countmap(datvec) k = string.(collect(keys(cmap))) v = collect(values(cmap)) ptab = DataFrame([k v], [:key, :counts]) sort!(ptab, :counts, rev=true) return ptab end make\_pivottable(somevector)

What is somevector?
