# Chocolate Data 2 - Convert to factors

### Function Definitions

# updateToFactor - Converts the numeric column to a factor
# Data frame column dfc and list of replacement values v passed to function
# Returns the factor form of the data column

updateToFactor<-function(dfc,v)
{
  # dfc is currently a numeric vector
  # We want to generate a character vector, so create a new NULL vector
  # that can have the appropriate elements added to it
  
  newValues<-NULL
  
  # Loop through each element of dfc and add the appropriate value
  # from v to the end of newValues to get a vector of characters that is
  # the same length as the original dfc column
  
  for(i in dfc)
  {
    newValues<-c(newValues,v[i])
  }

  # Convert newValues to a factor
  
  newValues<-factor(newValues)
  
  # Return newValues
  
  return(newValues)
}

# updateToFactor - Converts the numeric column to a factor
# Data frame column dfc and list of replacement values v passed to function
# Returns the factor form of the data column

updateCodeToFactor<-function(dfc,v)
{
  # dfc is currently a numeric vector
  # We want to generate a character vector, so create a new NULL vector
  # that can have the appropriate elements added to it
  

  
  # Loop through each element of dfc and add the appropriate value
  # from v to the end of newValues to get a vector of characters that is
  # the same length as the original dfc column
  
  # This is not the most elegant way of doing this, but it's the easiest
  # The original Excel file gives an incorrect set of codes - 
  # HA should be HCA and P should be PT
  

  
  # Convert newValues to a factor
  

  
  # Return newValues
  

}

# generateSummary - Counts how many of each chocolate were eaten where
# Creates a data frame to record the sweets eaten in each location
# Data frame df passed to function
# Returns the data frame

# This needs a data frame that contains 8 rows and 3 columns
# Column 1 - each hospital name twice
# Column 2 - each chocolate brand for each hospital
# Column 3 - the number of chocolates of that brand eaten at that hospital

generateSummary<-function(df)
{

  # Create a vector hospitals containing the hospital names
  

  
  # Create a vector chocolates containing the chocolate brands
  

  
  # Create a vector numberEaten containing zeros (to be updated)
  

  
  # Bind the three vectors into a data frame called summary

  
  # Loop through each record in the df data frame
  

    # Check which hospital name and chocolate brand the line refers to
    # If the chocolate is "Eaten" then update the appropriate entry
    # in the summary table (8 IF statements, one per line of summary)

  # Return the completed summary data frame to the main script
  

}


### Main Script

# Load data file into data frame ChocolateData
# We want to work with categorical data, so use stringsAsFactors=TRUE

chocolateData<-read.csv("SP15/ChocolateDataCSV.csv", header=TRUE, stringsAsFactors = FALSE)

# Define the values to replace the numbers in the column to be factorised



# Call updateCodeToFactor with WhoAte and newValues



# Update Location to become a factor

# Define the values to replace the numbers in the column to be factorised

newValues<-c("Bedford AAU","Bedford Pilgrim","Swindon Meldon","UHNS Haem")

# Call updateToFactor with Location and newValues

chocolateData$Location<-updateToFactor(chocolateData$Location,newValues)

# Define the values to replace the numbers in the column to be factorised

newValues<-c("Roses","Quality Street")

# The $Chocolate column starts at 0, not 1, so needs to be updated
# Add 1 to every element in chocolateData$Chocolate

chocolateData$Chocolate<-chocolateData$Chocolate+1

# Call updateToFactor with $Chocolate and newValues

chocolateData$Chocolate<-updateToFactor(chocolateData$Chocolate,newValues)

# Define the values to replace the numbers in the column to be factorised



# The $Eaten column starts at 0, not 1, so needs to be updated
# Add 1 to every element in chocolateData$Eaten



# Call updateToFactor with $Eaten and newValues



# Generate summary



# Display the summary data


