Calculator formulas: compute a new column from your data

Updated

The calculator computes a new column from a formula, row by row: the deviation of each part from nominal, a flag for parts out of tolerance, a ratio of two columns, a unit conversion, the time between two dates. It works like a spreadsheet formula filled down a whole column, with two differences that matter for statistics: missing values stay missing instead of turning into zeros, and the formula is written once and recorded, so you can always see how a column was made.

When to use it

  • You need a derived measurement: deviation from nominal, flatness from two heights, a ratio, a percentage.
  • You want to convert units, such as inches to millimeters or minutes to hours.
  • You want a 0/1 flag for a condition, such as out of tolerance or above a threshold, to count or to filter on.
  • You need to standardize a column or compare each value with the column mean.
  • You want the time between two events, such as from molding to inspection, from two date columns.

For a single quick answer rather than a column, use the super calculator.

How to do it in StatLab

  1. Choose Calc › Calculator….
  2. Under Store the result in, keep New column and give it a name in Name of the new column (optional), or pick an existing column to overwrite (it keeps its name).
  3. Type the Formula. To add a column or a function where the cursor is, choose it under Insert a column or Insert a function and press Insert.
  4. Watch the Preview. It shows the kind of result and its first values, such as Numbers, 8 values: 0.012, -0.013, …, or what is wrong and where.
  5. Press OK (or Enter in the formula box; Shift+Enter starts a new line). Results records which formula went into which column. Ctrl+Z undoes it.

The formula language in brief:

  • Columns: C1, or the name in single quotes, 'Wall thickness'. A name of only letters, digits and _ works without quotes: Width.
  • Numbers always use a decimal point (2.5), whatever your region. Text goes in double quotes: "Line A".
  • Operators: + - * /, ^ for powers, & to join text, comparisons = <> < <= > >= (true is 1, false is 0), and AND, OR, NOT.
  • Row functions work on each row: ABS, SQRT, ROUND(x, digits), IF(condition, a, b), ISMISSING(x), LN, LOG10, text functions and date functions such as WEEKDAY and DATE(year, month, day).
  • Column functions reduce a column to one value that applies to every row: MEAN, MEDIAN, STDEV, SUM, MIN, MAX, RANGE, N, NMISS, COUNT. Missing values are skipped.
  • Probability functions such as NORMCDF and TINV work row by row too.
  • Missing values carry through: a missing value in a row gives a missing result in that row. Undefined results, such as dividing by zero, are missing as well.

Example

A stamping press makes brackets with a nominal width of 25.40 mm and a tolerance of ±0.05 mm. A technician measures eight brackets; bracket 5 was bent and could not be measured.

Before:

C1 PartC2 Width
125.412
225.387
325.441
425.398
5*
625.456
725.403
825.371

Deviation from nominal. New column named Deviation, formula:

'Width' - 25.4

The preview reads Numbers, 8 values: 0.012, -0.013, 0.041, -0.002, *, 0.056, 0.003, -0.029. Results: Stored 'Width' - 25.4 in C3.

Out-of-tolerance flag. New column named Out:

IF(ABS('Width' - 25.4) > 0.05, 1, 0)

Standardized width, rounded to two decimals. New column named z:

ROUND(('Width' - MEAN(Width)) / STDEV(Width), 2)

This is the usual z-score, with the column's own mean and standard deviation:

zᵢ = (xᵢ − x̄) / s

Here x̄ = 25.40971 mm and s = 0.02979 mm, from the seven measured brackets.

After:

C1 PartC2 WidthC3 DeviationC4 OutC5 z
125.4120.01200.08
225.387-0.0130-0.76
325.4410.04101.05
425.398-0.0020-0.39
5****
625.4560.05611.55
725.4030.0030-0.23
825.371-0.0290-1.3

Bracket 6 is the only one out of tolerance, 0.056 mm above nominal. Bracket 5 stays missing in every new column: the calculator does not guess.

How to read the results

  • Read the preview before OK. It shows the type (numbers, text or dates) and the first values. A wrong type, such as text where you expected numbers, usually means a quoted value or a text column in the formula.
  • Check missing results. A * in a new column means a value in that row was missing or the result was undefined. Count them with NMISS if there are many.
  • Keep the Results line. It records the exact formula, so a reviewer can check how Deviation was computed.
  • Sum a 0/1 flag to count. SUM(Out) in the super calculator gives 1, the number of brackets out of tolerance.

Common mistakes

  • Typing a decimal comma. In formulas, numbers always use a point: 25.4, not 25,4. The comma separates the values of a function.
  • Forgetting quotes around names with spaces. Wall thickness - 2 does not work; 'Wall thickness' - 2 does. Insert a column writes the reference for you.
  • Text with the wrong capitals. Line = "line a" is false for Line A. Text comparisons are exact.
  • Expecting a fixed value from a column function. MEAN(Width) is recomputed from the column each time you run a formula. If you need the mean of a reference period, compute it once and type the number in.
  • Overwriting the source column. Storing the result in the column the formula reads replaces the raw data. Use a new column unless you are sure.

Frequently asked questions

How do I compute the time between two dates?

Subtract them: Inspected - Molded gives days, with fractions for the time of day. Multiply by 24 for hours. Adding a number to a date adds days: Molded + 7.

Why does my formula give a single value?

A formula made only of column functions and numbers, such as MEAN(Width), gives one value, so the new column holds a single value in row 1. Combined with a column, as in 'Width' - MEAN(Width), that value applies to every row. For one value you want to read rather than store, the super calculator is handier.

Are function names in English?

Yes, in every language, as in English-language spreadsheets: MEAN for the average, IF, ROUND. The Insert a function list shows every function, so you do not need to remember them.

Does the calculator update when the data change?

No. It stores values, not a live formula. Run it again after you change the data; the Output pane keeps the formula so you can copy it.

Try it with your own data

StatLab SPC is free, open-source statistics for engineers. It runs in your browser, works offline, and never uploads your data.

Open StatLab