nalimilan / FreqTables.jl

Frequency tables in Julia
Other
90 stars 19 forks source link

Support multidimensional #43

Closed nico202 closed 4 years ago

nico202 commented 5 years ago

in the spirit of what R does (returns an "Array" of tables)

dat <- data.frame("id" = 1:12, "A" = 1:3, "B" = 1:4)

table(dat$id, dat$B, dat$A)
, ,  = 1

     1 2 3 4
  1  1 0 0 0
  2  0 0 0 0
  3  0 0 0 0
  4  0 0 0 1
  5  0 0 0 0
  6  0 0 0 0
  7  0 0 1 0
  8  0 0 0 0
  9  0 0 0 0
  10 0 1 0 0
  11 0 0 0 0
  12 0 0 0 0

, ,  = 2

     1 2 3 4
  1  0 0 0 0
  2  0 1 0 0
  3  0 0 0 0
  4  0 0 0 0
  5  1 0 0 0
  6  0 0 0 0
  7  0 0 0 0
  8  0 0 0 1
  9  0 0 0 0
  10 0 0 0 0
  11 0 0 1 0
  12 0 0 0 0

, ,  = 3

     1 2 3 4
  1  0 0 0 0
  2  0 0 0 0
  3  0 0 1 0
  4  0 0 0 0
  5  0 0 0 0
  6  0 1 0 0
  7  0 0 0 0
  8  0 0 0 0
  9  1 0 0 0
  10 0 0 0 0
  11 0 0 0 0
  12 0 0 0 1

(it supports any number of columns, like table(dat$id, dat$B, dat$A, dat$C))

This is not something super-easy with FreqTables.jl, it would be nice if support can be added.

nalimilan commented 4 years ago

That already works AFAICT:

julia> using FreqTables

julia> x = repeat(["a", "b", "c", "d"], outer=[100]);

julia> y = repeat(["A", "B", "C", "D"], inner=[10], outer=[10]);

julia> z = repeat(["A", "B", "C", "D"], inner=[5], outer=[20]);

julia> freqtable(x, y, z)
4×4×4 Named Array{Int64,3}

[:, :, Dim3=A] =
Dim1 ╲ Dim2 │  A   B   C   D
────────────┼───────────────
a           │ 20   0  20   0
⋮              ⋮   ⋮   ⋮   ⋮
d           │ 10   0  10   0

[:, :, Dim3=B] =
Dim1 ╲ Dim2 │  A   B   C   D
────────────┼───────────────
a           │ 10   0  10   0
⋮              ⋮   ⋮   ⋮   ⋮
d           │ 10   0  10   0

[:, :, Dim3=C] =
Dim1 ╲ Dim2 │  A   B   C   D
────────────┼───────────────
a           │  0  10   0  10
⋮              ⋮   ⋮   ⋮   ⋮
d           │  0  10   0  10
⋮
nico202 commented 4 years ago

Yes. For some reason (maybe I was using an outdated package) it was not working. I'm sorry, closing