The Reshape Tab

The Reshape tab converts the table of a dataset between Wide format and Long format and saves the result as a Derived Dataset. A Wide format table places several measurements of the same individual in one row, one measurement per column. A Long format table places one measurement in each row and has a column that shows what was measured and a column that holds the measured value.

The following table shows the scores of three students in three subjects in Wide format. This page uses this table as its example.

namemathscienceenglish
Alice908588
Bob759280
Carol887895

In Long format, each row of this table holds the score of one student in one subject. The following table is the Long format table, with the subject column named subject and the score column named score.

namesubjectscore
Alicemath90
Alicescience85
Aliceenglish88
Bobmath75
Bobscience92
Bobenglish80
Carolmath88
Carolscience78
Carolenglish95

Which format to convert to depends on the format that the analysis or column operation you use after the conversion assumes. The GLMM tab assumes Long format, with one measurement in each row and a column that identifies the individual. On the other hand, an expression in Data > Add Columns... computes the value of a new column from the values of columns in the same row, as in "science" - "math". To add a column with the difference between two subject scores for each student, you therefore need a Wide format table, where the two measurements are in the same row. The Student's Sleep section of Sample Datasets shows how to convert a Long format table to Wide format with one row per participant and then compute the difference between two measurements.

Workflow

A conversion in the Reshape tab follows these steps.

  1. Open the Reshape tab with Data > Reshape (Wide/Long)...
  2. In Dataset, choose the dataset to convert
  3. At the top of the tab, choose Wide to Long or Long to Wide
  4. Specify the columns to use for the conversion
  5. Press Preview and check the number of rows and columns of the result and its first 100 rows
  6. Press Save as Dataset, give the dataset a name, and save it

Wide to Long

Wide to Long moves the values of the columns specified in Value Columns (to unpivot) into one column and splits each original row into as many rows as there are Value Columns. The resulting rows follow the order of the original rows, and the rows split from one original row follow the order in which the Value Columns were specified. The following screen shows the result of converting the example table. name is specified in ID Columns (keep as-is), and math, science, and english in Value Columns. Variable Column Name is set to subject, and Value Column Name to score. Each of the 3 original rows is split into 3 rows, and the result has 9 rows.

Wide to Long settings and preview

The result has three kinds of columns: ID columns, a variable name column, and a value column. The ID columns are the columns specified in ID Columns. Wide to Long copies the values of the ID columns of an original row to every row split from that row. The ID columns have the same data types and measurement scales as the original columns. The variable name column is named with the name entered in Variable Column Name, and each of its cells holds, as a string, the name of the original column the value was taken from. The measurement scale of the variable name column is nominal. The value column is named with the name entered in Value Column Name, and each of its cells holds the value of the original cell. Columns specified in neither ID Columns nor Value Columns are not included in the result.

Wide to Long also creates rows from cells with missing values and leaves the value column cell of such a row missing.

In Variable Column Name and Value Column Name, enter names that differ from each other and from the names of all ID columns. If names overlap, Preview shows an error. Wide to Long treats names that differ only in ASCII uppercase and lowercase as overlapping.

Data Type of the Value Column

Because the value column has a single data type, Wide to Long unifies the data types when the Value Columns have different data types. The resulting data type depends on the combination, as follows.

  • Wide to Long unifies a combination of int64 and float64 to float64
  • Wide to Long unifies a combination of date and datetime to datetime
  • Wide to Long unifies any other combination to string

When all Value Columns are enum columns of the same enum definition, the value column also becomes an enum column of that enum definition, and its categories are ordered by the enum definition order.

When Wide to Long unifies the data type to string, the value column can no longer be used as numbers or dates in computations.

Wide to Long does not use the measurement scales of the original columns; it determines the measurement scale of the value column from the data type of the value column. Data Types and Measurement Scales describes the rules for determining the measurement scale from the data type, and The Data Table Tab describes how to change the measurement scale.

Long to Wide

Long to Wide creates a column for each value of Variable Column (column names) and combines rows with the same combination of values in ID Columns (row identifiers) into one row. If no ID Columns are specified, Long to Wide combines all rows into one row. The following screen shows the result of converting the Long format table created in the Wide to Long example. name is specified in ID Columns, subject in Variable Column, and score in Value Column (cell values). The 9 rows are combined into one row per name, and each of the subject values english, math, and science becomes a column.

Long to Wide settings and preview

The result has two kinds of columns: ID columns and a per-value column for each value of Variable Column. The ID columns are the columns specified in ID Columns and have the same data types and measurement scales as the original columns. Each per-value column is named with a value of Variable Column, and its cells hold values of Value Column. The per-value columns have the same data type and measurement scale as Value Column. Columns specified in none of ID Columns, Variable Column, and Value Column are not included in the result.

The order of the per-value columns does not depend on the order of rows in the original table. When Variable Column is an enum column, the columns follow the enum definition order. The column created from missing values comes last regardless of the column type. Long to Wide does not create columns for enum definition values that do not appear in the original table.

For each combination of ID Columns values and a Variable Column value that has no row in the original table, Long to Wide leaves the corresponding cell missing. For example, if the example Long format table had no row for Carol's english score, Long to Wide would leave the english column of Carol's row in the result missing.

If two or more rows have the same combination of ID Columns values and the same Variable Column value, Preview shows an error. Long to Wide does not aggregate the values of such rows into one cell. If the data has a column that makes the combinations unique, add that column to ID Columns. To combine the values of the same combination, for example by their mean, aggregate them in the SQL Query Editor tab or the Crosstab tab before the conversion.

Column Names

Long to Wide converts Variable Column values to strings to use them as column names, even when the values are numbers or dates. If Variable Column has a missing value in some row, Long to Wide creates a column named NULL.

If the name of a per-value column overlaps with the name of an ID column or another per-value column, Long to Wide appends a sequence number such as _1 or _2 to the name of the per-value column. Long to Wide treats names that differ only in ASCII uppercase and lowercase as overlapping. When per-value columns overlap with each other, the sequence number goes to the column that comes later in the column order. For example, if a string Variable Column has the values a and A, A comes first, so the two columns are named A and a_1. However, if Variable Column has the value Row #, Preview shows an error instead of a result with a sequence number.

Saved Datasets

A Derived Dataset saved with Save as Dataset holds the conversion settings and depends on its input dataset. When you change the contents or definition of the input dataset, MIDAS rebuilds the saved Derived Dataset with the same settings. Datasets and Dependencies describes dependencies and recomputation.

To change the conversion settings of a saved Derived Dataset, right-click the dataset in the Project Lineage tab and open the Reshape tab with Edit Operation.... Change the settings, press Preview, and then press Update Dataset. When you press Update in the dialog that opens, MIDAS updates the Derived Dataset with the new settings.