Stack columns: turn wide data into one column with groups

Updated

Stacking puts several columns one under the other in a single new column, and can add a second column that says which original column each value came from. It turns wide data, with one column per group (Before, After; Supplier A, Supplier B, Supplier C), into stacked data, with one column of values and one column of group names. Most statistical tools, from a boxplot by group to one-way ANOVA, work best with the stacked layout.

When to use it

  • Data arrives with one column per machine, cavity, supplier or condition, and you want to compare the groups in one graph or test.
  • You have Before and After columns from an improvement trial with different numbers of parts.
  • You want to add a new group later by typing rows at the bottom, rather than by adding a column.
  • You want a single column of all the values to describe the whole population, with the group still recorded beside each value.

The reverse, splitting one column into a column per group, is Unstack column.

How to do it in StatLab

  1. Select a cell in one of the columns to stack, or select several columns. The dialog starts from your selection.
  2. Choose Data › Stack columns….
  3. Under Columns to stack, choose the columns. Hold Ctrl (⌘ on a Mac) or Shift to choose several. They are stacked in worksheet order, left to right.
  4. Set Name of the stacked column. It is Stacked unless you change it.
  5. Keep Also store which column each value came from checked to get the group column, and set Name of that column (Source by default).
  6. Press OK. The new columns go after the last column in use. Results records how many columns were stacked and where, and Ctrl+Z undoes it.

What goes into the new columns:

  • The values of the leftmost chosen column, then the next one, and so on, top to bottom.
  • The group column holds each value's column name (or its label, such as C2, if the column has no name).
  • Columns can have different lengths. A column ends at its last value, so blank cells below it are not stacked; missing values (*) inside a column are stacked as missing.
  • The original columns stay as they were.
  • Stacking numbers with text gives a text column, since one column holds one type.

Example

A machining cell suspects its shaft-holding fixture causes runout. It measures total runout (mm) on six shafts with the old fixture and five with a new one, and records them side by side:

C1 Old fixtureC2 New fixture
0.420.31
0.380.27
0.450.33
0.410.29
0.470.34
0.36

In Data › Stack columns… the engineer chooses both columns, names the stacked column Runout (mm), keeps Also store which column each value came from checked and names that column Fixture. Results: Stacked 2 columns into C3.

After:

C1 Old fixtureC2 New fixtureC3 Runout (mm)C4-T Fixture
0.420.310.42Old fixture
0.380.270.38Old fixture
0.450.330.45Old fixture
0.410.290.41Old fixture
0.470.340.47Old fixture
0.360.36Old fixture
0.31New fixture
0.27New fixture
0.33New fixture
0.29New fixture
0.34New fixture

C3 holds all eleven runouts and C4 says which fixture produced each. The blank cell at the bottom of New fixture was not stacked: that column simply ends after five values. This layout is ready for a boxplot of Runout by Fixture or a 2-sample t-test:

Shaft runout by fixture0.300.350.400.45Runout (mm)Old fixtureNew fixture
Shaft runout by fixture

The old fixture's runouts average 0.415 mm and the new one's 0.308 mm, and the two groups do not overlap. A formal test would tell you how sure you can be, but the graph already says where to look.

How to read the results

  • Count the rows. The stacked column should have as many values as the chosen columns together (here 6 + 5 = 11). Fewer means a column ended earlier than you thought.
  • Check the group column. It should show every group name, each in one block, in the order of the columns in the worksheet.
  • Check the type. A stacked column of measurements labelled -T (text) means one of the columns contained text. Fix it in the source column, then stack again.

Common mistakes

  • Stacking columns measured in different units. Stacking diameters in mm with diameters in inches gives a column that means nothing. Convert first with the calculator.
  • Stacking paired data and forgetting the pairs. If Before and After are the same parts measured twice, the stacked layout loses which rows belong together. For a paired t-test, keep the two columns side by side, or add a part column before stacking.
  • Leaving the group column out. Without Also store which column each value came from, you get one column of values with no way to tell the groups apart. Keep it checked unless you really want one pooled column.
  • Mixing text into numbers. A single n/a in a source column makes the stacked column text. Clear it or use * for missing.

Frequently asked questions

Can the columns have different numbers of values?

Yes. Each column contributes the values it has. Missing values (*) inside a column are carried over as missing.

Does stacking delete the original columns?

No. They stay as they were. Delete them with Data › Delete columns if you no longer need them.

What if a column has no name?

The group column then holds its label, such as C2. Name your columns before stacking to get readable group names.

How do I get back to one column per group?

Use Data › Unstack column… on the stacked column, with the group column as Groups (subscripts).

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