Checking for Balance

The following code shows how to compute the number of observations in each cell of our experimental design.

load(url("http://pnb.mcmaster.ca/bennett/psy710/datasets/L5dat1.Rdata") )
with(L5.dat.1,tapply(y,list(A,B),length) )
##    b1 b2 b3
## a1  6  6  6
## a2  6  6  6
xtabs(~A+B,data=L5.dat.1)
##     B
## A    b1 b2 b3
##   a1  6  6  6
##   a2  6  6  6

Subset

The following code shows how to select a subset of data, specifically all of the rows of L5.dat.1 that have a value of b1 on factor B.

levels(L5.dat.1$B) # list names of levels
## [1] "b1" "b2" "b3"
subset(L5.dat.1,B=="b1") # get subset
##            y  A  B
## 1  14.667215 a1 b1
## 2  13.004475 a1 b1
## 3  11.066173 a1 b1
## 4   7.544869 a1 b1
## 5   8.713667 a1 b1
## 6  13.452669 a1 b1
## 19  7.708481 a2 b1
## 20 13.637747 a2 b1
## 21  9.956165 a2 b1
## 22 10.685710 a2 b1
## 23 13.525651 a2 b1
## 24 13.523859 a2 b1

The following code shows how to select all of the rows of L5.dat.1 that have a value of a1 on factor A and a value of b1 on factor B.

subset(L5.dat.1,A=="a1"&B=="b1") # get subset
##           y  A  B
## 1 14.667215 a1 b1
## 2 13.004475 a1 b1
## 3 11.066173 a1 b1
## 4  7.544869 a1 b1
## 5  8.713667 a1 b1
## 6 13.452669 a1 b1

2-Way Tables

The following code shows how to create tables listing the mean and standard deviation for each condition.

with(L5.dat.1,tapply(y,list(A,B),mean))
##          b1       b2       b3
## a1 11.40818 11.17105 17.35382
## a2 11.50627  7.93458 11.45721
with(L5.dat.1,tapply(y,list(A,B),sd))
##          b1       b2       b3
## a1 2.816095 2.214404 1.873691
## a2 2.457279 1.967062 3.899145

Marginal Means

The following code calculates the marginal means for A and B.

with(L5.dat.1,tapply(y,A,mean)) # marginal means for A
##       a1       a2 
## 13.31101 10.29935
with(L5.dat.1,tapply(y,B,mean)) # marginal means for B
##        b1        b2        b3 
## 11.457224  9.552812 14.405511

interaction Command

The interaction command can be used to create a factor that contains a unique level for each condition.

L5.dat.1$cellID <- interaction(L5.dat.1$A,L5.dat.1$B)
summary(L5.dat.1)
##        y           A       B        cellID 
##  Min.   : 5.185   a1:18   b1:12   a1.b1:6  
##  1st Qu.: 8.862   a2:18   b2:12   a2.b1:6  
##  Median :11.297           b3:12   a1.b2:6  
##  Mean   :11.805                   a2.b2:6  
##  3rd Qu.:13.912                   a1.b3:6  
##  Max.   :19.491                   a2.b3:6