Stacking a dataset means to convert it from unstacked form to stacked form.
To illustrate the difference between the two forms, consider the
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.
To convert a dataset from unstacked to stacked form, use the
To stack only some of the columns in your dataset, use the
select argument. For example, to stack only the
ClassC variables, use the command:
> newdataset<-stack(grades1, select=c("ClassA", "ClassC"))
Note that you can only stack numeric variables.
stack function automatically names the new variables ‘
values’ and ‘
ind’, but you can change the names to something more informative with the
> names(grades2)<-c("Result", "Class")