Is there any smart way of doing pivot table?

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?

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)
1 Like

What is somevector?