# 9-6 Exercise 2 - Working with factors

# The data is held in the file ThreeMonths.csv
# This is within the SP09 folder of the current working directory
# The file contains headers

# Load the file file

threeMonths<-read.csv("SP09/ThreeMonths.csv", header=TRUE)

# Display threeMonths

threeMonths

# Display data frame headers

names(threeMonths)

# Display mean attendance for each month

tapply(threeMonths$Attendances, 
       threeMonths$Month,
       mean)

# Display mean attendance for each month and size

tapply(threeMonths$Attendances, 
       list(threeMonths$Month,threeMonths$Size),
       mean)

# Display mean attendance for each month, size, and target

tapply(threeMonths$Attendances, 
       list(threeMonths$Month,threeMonths$Size,threeMonths$Target),
       mean)

# Display under4percentage for each month

split(threeMonths$Under4Percentage, 
       threeMonths$Month)

# Display under4percentage for each month and target

split(threeMonths$Under4Percentage, 
      list(threeMonths$Month,threeMonths$Target))

# Display under12percentage for each month

as.data.frame(split(threeMonths$Under12Percentage, 
      threeMonths$Month))
