Unstack a column: split values into one column per group
Unstacking splits one column of values into several columns, one per group, using a second column that says which group each value belongs to. It is the reverse of stacking: a column of weights with a cavity column beside it becomes Weight_1, Weight_2, Weight_3. Use it when a tool, a report or your own eyes need the groups side by side.
When to use it
- You want to see each group's values next to each other, for example to compare cavities or heads at a glance.
- A colleague's spreadsheet or a report template expects one column per machine, cavity or supplier.
- A tool needs groups in separate columns, such as a paired t-test on two columns or a correlation between two groups measured on the same occasions.
- You want to copy one group's values out quickly without filtering.
For most comparisons of groups (boxplots by group, ANOVA, 2-sample tests) the stacked layout is already right, and you do not need to unstack.
How to do it in StatLab
- Select a cell in the column of values. The dialog starts from it.
- Choose Data › Unstack column….
- Check Column to unstack: the column of values.
- Choose Groups (subscripts): the column that says which group each row belongs to. Each different value in it becomes a new column.
- Press OK. Results records how many columns were made, and Ctrl+Z undoes it.
How the new columns are built:
- They go after the last column in use, in the order the groups first appear in the groups column, not in sorted order.
- Each is named after the original column and the group: Weight_1, Weight_A. If a name is taken, a number is added.
- Within each new column the values keep their original top-to-bottom order.
- Rows with no group are left out. A missing value in the groups column means the row cannot be placed.
- Missing values in the values column are kept in their group, as *.
- The original columns stay as they were.
Example
A 3-cavity injection mold makes clips. A technician weighs twelve clips (g) and notes the cavity number molded into each one. One clip from cavity 2 broke before weighing, and on one clip the cavity number was unreadable.
Before:
| C1 Cavity | C2 Weight |
|---|---|
| 1 | 18.42 |
| 2 | 18.61 |
| 3 | 18.37 |
| 1 | 18.45 |
| 2 | 18.66 |
| 3 | 18.35 |
| 1 | 18.41 |
| 2 | * |
| 3 | 18.39 |
| * | 18.52 |
| 1 | 18.44 |
| 2 | 18.63 |
In Data › Unstack column… she chooses Weight as Column to unstack and Cavity as Groups (subscripts). Results: Unstacked C2 Weight into 3 columns.
The new columns (C1 and C2 are unchanged):
| C3 Weight_1 | C4 Weight_2 | C5 Weight_3 |
|---|---|---|
| 18.42 | 18.61 | 18.37 |
| 18.45 | 18.66 | 18.35 |
| 18.41 | * | 18.39 |
| 18.44 | 18.63 |
Cavity 1 has four weights. Cavity 2 has four entries, one of them missing. Cavity 3 has three. The clip with the unreadable cavity number (18.52 g) does not appear in any new column, because it cannot be assigned to a cavity.
Side by side, the pattern is plain: cavity 2 clips average 18.63 g, against 18.43 g for cavity 1 and 18.37 g for cavity 3.
Cavity 2 deserves a look: a larger gate, a hotter zone or a worn insert would all make clips heavier.
How to read the results
- Check the number of new columns. It equals the number of different group values. One more than expected usually means an inconsistent code in the groups column, such as
Cav 2next to2. - Add up the values. The new columns together should hold every row that has a group. Here 4 + 4 + 3 = 11 of the 12 rows; the missing one has no cavity.
- Look for * in the new columns. They are measurements that are missing, not empty space. Blank cells at the bottom of a shorter column are just the end of that column.
Common mistakes
- Reading rows across the new columns as matched. In the example, row 1 of Weight_1 and row 1 of Weight_2 are not the same shot of the mold; they are just the first clip of each cavity in the log. Only treat rows as pairs if the data really are paired, for example one clip from each cavity per shot, logged in the same order.
- Group codes that differ only in spelling.
Line A,line AandLine-Aare three different groups. Clean them first with Recode values. - Expecting sorted column order. New columns follow the order in which groups first appear. If you want Cavity 1, 2, 3 in that order, make sure a cavity 1 row comes first, or sort by the groups column before unstacking.
- Losing rows without a group. Rows with a missing group value are left out silently. Count them before you unstack, for example with
NMISS(Cavity)in the super calculator.
Frequently asked questions
What is the difference between stack and unstack?
Stack turns several columns into one column of values plus a group column. Unstack turns one column of values plus a group column into one column per group. Each undoes the other.
Can the groups column hold text, numbers or dates?
Any of them. Each different value becomes a group, and its name appears in the new column's name, such as Weight_A or Weight_3.
Why are some new columns shorter than others?
Each group gets as many values as it had rows. Groups do not need equal sizes.
Does unstacking change the original columns?
No. The values and groups columns stay as they were.