Transposing and Reshaping Data

KEY:

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

_____________________________

t(ds)

_____________________________

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.

_____________________________

library(reshape2)

XYXY.restruct <- dcast(ds,  ds$Subject ~ ds$ColCol,
                      value.var="PopulateVar")

_____________________________

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")

_____________________________