# 9-5 - tapply(), split()

# The data is held in the file FullPatientNumbers.csv

# This is within the SP09 folder of the current working directory
# The file contains headers

# Load FullPatientNumbers.csv file

patients<-read.csv("SP09/FullPatientNumbers.csv", header=TRUE)

# Display patients

patients

# Display the mean for each level of patients$Size

tapply(patients$Attendances,patients$Size, mean)

# Display the mean for each level of patients$Size
# and each value of patients$Target

tapply(patients$Attendances, 
       list(patients$Size, patients$Target),
       mean)

# Display the number of patients for each hospital in the
# levels of patients$Size (i.e. Major and Minor)

split(patients$Attendances,patients$Size)

# Display the number of patients for each hospital in the
# levels of patients$Size (i.e. Major and Minor) and of
# patients$Target (i.e. TRUE and FALSE)

split(patients$Attendances, 
       list(patients$Size, patients$Target))

# Display the number of patients for each hospital as a data frame

as.data.frame(split(patients$Attendances, patients$Hospital))

