Transposing and Reshaping Data


ds = dataset you are currently using.

DV = dependent variable of interest

IV = independent variable of interest


Subject = name of subject number column 

XYXY = dummy name for a variable, matrix, or data frame into which you are moving information.

Basic Transposition




Make columns based on rows


In this case you have 3 columns that are of interest. You have a subject number that will serve as a key for the rows (Subject). You have a column that you want to be divided into multiple columns (ColCol). You have a column that contains the values that will populate these new rows and columns (PopulateVar).

You will need the dcast function from the reshape2 package.



XYXY.restruct <- dcast(ds,  ds$Subject ~ ds$ColCol,


At this point you may want to rename your new columns. You can use this command


names(XYXY.restruct) <-c("Subject", "Name 1", "Name 2", "Name 3")


Stack Columns



XYXY <- cbind(ds[1], stack(ds[c(2,3,4)]))

names(XYXY)[c(2,3)] <- c("Score", "Variable")