# Programmer: Stephen Protheroe-Hewitt

# Title: Rollercoaster R Scripts

# Purpose: To create, analysis and define the following five tasks:

# Task One: Create the 'coasterData' data frame from the 'roller_coasters.csv' file
# Task Two: Create the 'highestData' data frame to show the name, height, park and status
# Task Three: Create the 'displayComparison' function to create a bar chart for inverted 
#             rollercoasters
# Task Four: Create the 'correlateLengthSpeed' function to create the 'lengthSpeed' 
#            data frame to show the mean height, length and coefficient of correlation
# Task Five: Create the 'compare WoodenSteel' function to perform a t-test of 0.05 
#            to compare wooden and steel rollercoaster speeds

# Month Written: March/April 2024




# Task One

# Creating the 'coasterData' data frame from the 'roller_coasters.csv' file with headers

coasterData<-read.csv("roller_coasters.csv", header=TRUE)

# Converting type, material, manufacturer and current status from character to factors

coasterData$material_type <- factor(coasterData$material_type)
coasterData$seating_type <- factor(coasterData$seating_type)
coasterData$manufacturer <- factor(coasterData$manufacturer)
coasterData$status <- factor(coasterData$status)

# Checking the type, material, manufacturer and current status are factors

class(coasterData$material_type)
class(coasterData$seating_type)
class(coasterData$manufacturer)
class(coasterData$status)

# Displaying the 'coasterData' data frame

print(coasterData)





# Task Two

# Filtering 'coasterData' for the status of 'status.operating' only

operating <- coasterData[coasterData$status == 'status.operating', ]

# Creating the 'highestData' data frame to display the highest operating roller coaster 
# of each type

highestData <- data.frame('Name' = coasterData$name, 
                          'Height' = coasterData$height,
                          'Park' = coasterData$park,
                          'Status' = coasterData$status,
                          'Material_type' = coasterData$material_type)

# Finding the highest operating roller coaster for each material type

highestData <- aggregate(height ~ material_type, data = operating, FUN = max)

# Merging the original data to retrieve the highest roller coaster details

highestData <- merge(coasterData, highestData, by = c("material_type", "height"))

# Selecting the required columns for the 'highestData' data frame

highestData <- highestData[, c("name", "height", "park", "status", "material_type")]

# Displaying the 'highestData' data frame

print(highestData)





# Task Three

# Creating the 'displayComparison' function to display the 'seating_type' 
# comparison

displayComparison <- function(seating_type) {
  
  # Filtering the operating rollercoasters by seating type
  
  operating_coasters <- coasterData[coasterData$status == 'status.operating' & coasterData$seating_type == seating_type, ]
  
  # Sorting the operating rollercoasters by 'speed' and selecting the top 10
  
  top_10_fastest <- head(operating_coasters[order(-operating_coasters$speed), ], 10)
  
  # Creating a bar chart of the fastest 10 rollercoasters by speed
  
  barplot(top_10_fastest$speed, 
          names.arg = top_10_fastest$name, 
          horiz = FALSE, 
          col = "skyblue", 
          main = paste("Top 10 Fastest", seating_type, "Roller Coasters"),
          xlab = "Roller Coaster Name",
          ylab = "Speed")
}

# Displaying the 'displayComparison' function with the seating type 'Inverted'

displayComparison("Inverted")





# Task Four

# Creating the 'correlateLengthSpeed' function to calculate the correlation 
# between length and speed

correlateLengthSpeed <- function(correlation) {
  
  # Filtering for sit down rollercoasters only, removing any NA fields
  
  sit_down_coasters <- coasterData[coasterData$seating_type == 'Sit Down', ]
  sit_down_coasters <- sit_down_coasters[complete.cases(sit_down_coasters), ]
  
  # Calculating the mean height and length
  
  mean_height <- mean(sit_down_coasters$height)
  mean_length <- mean(sit_down_coasters$length)
  
  # Calculating the correlation coefficient between length and speed
  
  correlation <- cor(sit_down_coasters$length, sit_down_coasters$speed)
  
  # Creating the 'lengthSpeed' dataframe
  
  lengthSpeed <- data.frame(Mean_Height = mean_height,
                            Mean_Length = mean_length,
                            Correlation_Length_Speed = correlation)
  
}

# Adding the 'correlateLengthSpeed' function to the 'lengthSpeed' dataframe

lengthSpeed <- correlateLengthSpeed()

# Displaying the 'lengthSpeed' data frame

print(lengthSpeed)





# Task Five

# Creating the 'compareWoodenSteel' function to calculate a t-test between 
# wooden and steel rollercoaster speeds

compareWoodenSteel <- function(t_test) {
  
  # Filtering for wooden and steel rollercoasters only
  
  wooden_coasters <- coasterData[coasterData$material_type == 'Wooden', ]
  steel_coasters <- coasterData[coasterData$material_type == 'Steel', ]
  
  # Removing any NA values within the 'speed' column
  
  wooden_speeds <- wooden_coasters$speed[complete.cases(wooden_coasters$speed)]
  steel_speeds <- steel_coasters$speed[complete.cases(steel_coasters$speed)]
  
  # Performing a t-test between wooden and steel rollercoaster speeds
  
  t_test_result <- t.test(wooden_speeds, steel_speeds)
  
  # Printing the hypotheses and results of the t-test
  
  cat("Hypotheses:\n")
  cat("H0: There is no significant difference between the speeds of 
      wooden and steel roller coasters.\n")
  cat("H1: There is a significant difference between the speeds of 
      wooden and steel roller coasters.\n\n")
  
  cat("T-test Results (p=0.05):\n")
  
  # Displaying the results of the t-test
  
  print(t_test_result)
  
  # Checking if the p-value is less than 0.05 and printing the required 
  # conclusion
  
  if (t_test_result$p.value < 0.05) {
    cat("\nConclusion: Reject the null hypothesis. There is a significant 
        difference between the speeds of wooden and steel rollercoasters.\n")
                                    } 
  
  else {
    cat("\nConclusion: Fail to reject the null hypothesis. There is no significant 
        difference between the speeds of wooden and steel rollercoasters.\n")
       }
  
}

# Displaying the results of the 'compareWoodenSteel' function

compareWoodenSteel()
