Skip to main content

Stack Variables

Convert from table form (rows corresponding to subjects) to long form (rows corresponding to observations).

Stack variables

In many cases, the data may be in tabular form, in which multiple observations are made but placed in different columns. An example of this may be a study of blood pressure on patients using several medications. The columns of this data set may be: patient.id, gender, drug, Week1, Week2, Week3. Here, each patient has their own row in the data set, but each row contains three observations of blood pressure.

patient.idgenderdrugWeek1Week2Week 3
1maleA130125120
2maleB140130110
3femaleA120119116

We may want to convert to long form, where we have each observation in a new row, and use a categorical variable to differentiate the weeks. In this case, we would select Week1, Week2, and Week3 as the variables in the list. The new data set will have the columns patient.id, gender, drug, Stack.variable ("Week"), and stack.value ("blood pressure").

patient.idgenderdrugstack.variablestack.value
1maleAWeek1130
1maleAWeek2125
1maleAWeek3120
2maleBWeek1140
2maleBWeek2130
2maleBWeek3110
3femaleAWeek1120
3femaleAWeek2119
3femaleAWeek3116

Of course, you can rename the variables as appropriate using Manipulate Variables > Rename Variables.