Memra

From a numeric column to a distribution

◈ 9 cards

Bin the amounts at 0, 300, …, 1500 and the counts are 4 · 4 · 2 · 1 · 1 — a right-skewed, unimodal shape with one high value. COUNTIFS with >= and < is left-closed; FREQUENCY, cut() and hist() are right-closed.

A frequency table for a numeric variable

A numeric variable has too many distinct values to count one by one — every amount here is different. So you cut its range into classes (bins) of equal width and count how many values fall in each. The result is a frequency table like Lesson 4.1's, and its chart is the histogram: one bar per class, height equal to the count, bars touching because the classes are adjacent intervals of one continuous scale. That touching is the visual difference from a bar chart, whose bars stand apart because its categories are separate things.

Worked example — five classes of width 300

The amounts run from 90 to 1320. Classes of width 300 from 0 give five: [0, 300), [300, 600), [600, 900), [900, 1200), [1200, 1500). Put the lower edges in G2:G6 (0, 300, 600, 900, 1200) and the upper edges in H2:H6 (300, …, 1500). The count for row 2 is

=COUNTIFS(2:13, ">="&G2, 2:13, "<"&H2)

— two conditions on the same locked range, each criterion built by gluing an operator to a cell with &. Fill down: 4 · 4 · 2 · 1 · 1, summing to 12. The shape is readable at once: right-skewed (the counts fall away to the right), unimodal (one hump, in the first two classes), with one high value sitting alone in the last class — the $1,320 order again.

Sheets and Excel also have FREQUENCY(data, upper_edges), which returns the whole column at once — but its convention is up to and including each upper edge: (0, 300], (300, 600], …. The COUNTIFS above is left-closed (a value of exactly 300 counts in the second class); FREQUENCY is right-closed (300 would count in the first). The shared data has no value on any boundary, so the two agree here — and a lesson that bins data should say which convention it used, because a value on a boundary is the classic off-by-one.

The same histogram in R

> h <- hist(orders$amount, breaks = seq(0, 1500, 300), plot = FALSE)
> h$counts
[1] 4 4 2 1 1

hist() draws the picture by default; with breaks you set the edges (seq(0, 1500, 300) is 0, 300, …, 1500), and main = "Order amount" titles it. R's intervals are right-closed like FREQUENCY, with the lowest edge included. Leave breaks out and hist() chooses its own number of classes by Sturges' rule — about , five for twelve values — then rounds the edges to "pretty" numbers; on this data it picks seven classes of width 200 — edges 0, 200, …, 1400, counts 2 3 3 2 1 0 1. cut(orders$amount, seq(0, 1500, 300)) produces the class labels (0,300], (300,600], … for a table().

Choosing classes

Three rules of thumb keep a histogram honest. Use 5 to 15 classes — fewer hides the shape, more turns it into noise. Keep the widths equal, or bar heights stop meaning counts. Put the boundaries off the data values: if an amount of exactly 600 existed, a class edge at 600 would make its bin a matter of convention, and shifting the edges to 50, 350, … removes the ambiguity.

Type both calls, then bin the data

Type the COUNTIFS and the hist() call. The worksheet asks for the five counts; the code block bins the amounts with lo <= a < hi — the left-closed convention, stated.

CRISP-DM: binning and the histogram are data understanding → explore data.

ClassLower (G)Upper (H)Count10300423006004360090024900120015120015001Left-closed [lower, upper). Sum 12. R's hist(breaks = seq(0, 1500, 300)) returns the same counts becauseno amount sits on an edge.
Equal-width classes of 300 for `amount`, counted with `=COUNTIFS($D$2:$D$13, ">="&G2, $D$2:$D$13, "<"&H2)`. The counts fall away to the right: right skew.
NORMAL ~/memra/learn/afm-112/binning-and-the-histogram utf-8 LF