Category Archives: Data preparation & manipulation

Stacking a dataset in R

Stacking a dataset means to convert it from unstacked form to stacked form.

To illustrate the difference between the two forms, consider the grades1 and grades2 datasets shown below. The grades1 dataset is in unstacked form. It gives the test results of 15 students, arranged in separate columns according to which class they belong to. The grades2 dataset gives the same data in stacked form. Here the data is arranged in two columns, the first giving the test result and the second identifying which class the student belongs to.

grades1: Dataset in unstacked form grades2: Dataset in stacked form
(a) grades1 (unstacked form) (b) grades2 (stacked form)

To convert a dataset from unstacked to stacked form, use the stack function.

> grades2<-stack(grades1)

To stack only some of the columns in your dataset, use the select argument. For example, to stack only the ClassA and ClassC variables, use the command:

> newdataset<-stack(grades1, select=c("ClassA", "ClassC"))

Note that you can only stack numeric variables.

The stack function automatically names the new variables ‘values’ and ‘ind’, but you can change the names to something more informative with the names function.

> names(grades2)<-c("Result", "Class")

Social Widgets powered by AB-WebLog.com.