Data stacking is a data preparation step where a data set is split into subsets, and the subsets are merged by case (or stacked on top of one another). The number of variables in the data decreases, and the number of cases increases. This is sometimes referred to as converting data from a wide format to a long format.
This article describes how to stack a data set in Q. There are two approaches that you can take:
- Use Tools > Stack SPSS .sav File. This gives you a drag-and-drop interface to organize your variables and save a new SPSS File.
- Import your data using File > Data Sets > Add > R. Q’s integration with R allows you to write code to pre-process your data during the importation step.
Requirements
A data file with multiple observations per case.
- If using Q's built-in tool: an unstacked SPSS data file
- If using R: an unstacked data file of any compatible type
Method
There are two approaches that you can take to stack your data in Q:
- Use Tools > Stack SPSS .sav File. This gives you a drag-and-drop interface to organize your variables and save a new SPSS File.
- Import your data using File > Data Sets > Add > R. Q’s integration with R allows you to write code to pre-process your data during the importation step.
A worked example
In this article we consider data from a simple survey which asked people about how they rate different technology brands. You can try this example for yourself by downloading the data filehere, or by using the code below. I’ve shown a subset of the data before and after stacking, below.
Before stacking, you should always look at your data and work out:
- Which variables do you want to stack?
- Which variable is the ID variable?
- Are there other variables in your data you want to include but not stack? These will bestretched, which means that their values will be repeated several times for each of the original cases.
- Which variables do you want to exclude from the new data?
Stacking data in Q using drag and drop
If the initial file is not an SPSS (.sav) data file, do the following:
- Open up Q and import the file into Q (File > Data Sets > Add to Project > From File).
- When prompted, make sure to select Use original data file structure, then click OK.
- Go to Tools > Save Data as SPSS/CSV file.
To stack an SPSS file:
- Start a new Q project: File > New Project.
- Add the SPSS data file to the project: File > Data Sets > Add to Project > From File, select the file, and press Open.
- When prompted, make sure to select Use original data file structure, then click OK.
- Select Tools > Stack SPSS .sav File to open the drag-and-drop stacking interface.
- Arrange your stacked variables so that the first variable from each set is placed under the column Observation 1, the second variable from each set falls under the column Observation 2, and so on. The most efficient way to do this is to drag multiple variables at once. In our example this is as follows. For each set of stacked variables:
- Locate the first variable in the set, for example Q3_01
- Highlight all of the other variables which you want to stack on top of that variable. For example, Q3_02 to Q3_13.
- Click and drag them to the right of the first variable, so that their names appear in columns for Observation 2,Observation 3, etc.
6. Drag any variables that you don’t want to include in the stacked file over in to the Omit section on the left. In our example we have omitted “Observation 14” as this pertains to a “None of these” response, rather than an actual brand. It is worth keeping an ID variable such as RESPNUM as a case identifier, and/or if you want to create relationships between datafiles.
7. Press OK. Choose an appropriate file name and location, and press Save.
8. Add the new data file to the project:File > Data Sets > Add to Project > From File, select the file, and press Open.
9. Go to the Variables and Questions tab for the new file, and select the observation variable (probably at the bottom of the file) and press the…button (in the middle of the screen):
- Enter the Label for each value (e.g., if the first variable was Apple, and the second was Microsoft, then enter these in the first two rows).
- Check the Missing Data option for any categories you want to exclude, such as None of these.
- Press OK.
- Change the Label for observation to Brand (or whatever the distinct observations represent in your study).
Stacking data in Q using code
To stack data in Q using R code, please see our How to Automatically Stack a Data Set article for more detail.
Next
How to Automatically Stack a Data Set
How to Set Up and Manage Data File Relationships
How to Quickly Make Data Long or Wide Using R