Skip to content

Instantly share code, notes, and snippets.

@hinjolicious
Created June 4, 2026 07:02
Show Gist options
  • Select an option

  • Save hinjolicious/4cfa52e3b4bccaecfff9c76e97a7b546 to your computer and use it in GitHub Desktop.

Select an option

Save hinjolicious/4cfa52e3b4bccaecfff9c76e97a7b546 to your computer and use it in GitHub Desktop.
Tabulize number list into bins and count frequencies - Useful for data plotting
Red [
Title: "Data Tabulation Function"
]
min-of: func [s [series!] /local m] [m: first s foreach v s [if v < m [m: v]] m]
max-of: func [s [series!] /local m] [m: first s foreach v s [if v > m [m: v]] m]
tabulize: func [
data [block!] "The input list of numbers"
num-bins [integer!] "The number of bins desired"
/set-min smin
/set-max smax
/plot-output
return: [block!] "Returns [[x-list] [y-list]]"
/local min-val max-val bin-size x-list y-list val bin-index
][
;; 1. Find the range of the data dynamically
min-val: either set-min [smin][min-of data]
max-val: either set-max [smax][max-of data]
;; Handle edge case where all numbers are identical
if min-val = max-val [max-val: max-val + 1]
;; Calculate the size of each bin
bin-size: (max-val - min-val) / num-bins
;; 2. Generate X List (the starting boundary of each bin)
x-list: collect [
repeat i num-bins [
keep (min-val + ((i - 1) * bin-size))
]
]
;; 3. Initialize Y List (frequencies set to 0)
y-list: copy/deep append/dup clear [] 0 num-bins
;; 4. Populate Frequencies
foreach val data [
;; Determine 1-indexed bin location
bin-index: to integer! ((val - min-val) / bin-size) + 1
;; Ensure data points exactly on max-val don't overflow the block boundary
if bin-index > num-bins [bin-index: num-bins]
if bin-index < 1 [bin-index: 1]
;; Increment frequency
poke y-list bin-index ((pick y-list bin-index) + 1)
]
;; 5. Return both lists nested in a single block
either plot-output [ ; plot ready output
collect [repeat i length? x-list [
keep x-list/:i keep y-list/:i
]]
][ ; separate x and y lists
reduce [x-list y-list]
]
]
comment {
num-gen: function [min max num][
random/seed now/time/precise
collect [ loop num [ keep min + random (max - min) ]]
]
dat: tabulize/set-min/set-max (num-gen 0.0 1.0 1000) 20 0.0 1.0
print mold dat/1
print mold dat/2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment