
### Task 1 Q1 ######################################################################################

# imports the csv and loads it as a dataframe, 
# the first row is turned in headers and some columns are imported as factors 
coasterData <- read.csv("roller_coasters.csv", header=T, stringsAsFactors=T)

# creates another dataframe that removes all rows that contains either NA or blanks cells
coasterDataNoNA <- coasterData[complete.cases(coasterData),]
coasterDataNoNA[coasterDataNoNA=="na"] <- NA
coasterDataNoNA <- coasterDataNoNA[complete.cases(coasterDataNoNA),]


# displays the data frame
print(coasterData) 


### Task 1 Q2 ######################################################################################

# creates a dataframe containing all the columns that are required
templateData <- data.frame (
  name = coasterData$name,
  park = coasterData$park,
  status = coasterData$status,
  height = coasterData$height,
  seating_type = coasterData$seating_type
)

# creates an empty  data frame that data will be added to
heighestData <- data.frame()

# gets all the unique seating types that will be cycled through
seatingTypes <- unique(templateData$seating_type)
# removes na from the seating types
seatingTypes <- seatingTypes[seatingTypes != "na"]

# loops through each seating type
for (i in seatingTypes) {
  # creates a subset of the template data whose criteria match what is needed
  filteredDataframe <- subset(templateData, 
                              templateData$seating_type == i # gets roller coaster of a certain seat type
                              & templateData$status == "status.operating") # is roller coaster active
  
  # gets the biggest value in a vector
  maxvalue <- max(filteredDataframe$height, na.rm=TRUE)
  # creates a subset of the filtered data on which rows have the same height as the biggest value
  filteredDataframe <- subset(filteredDataframe, filteredDataframe$height == maxvalue)
  
  # removes any rows which contain NA or blank values
  filteredDataframe <- filteredDataframe[complete.cases(filteredDataframe),]
  # binds the filtered dataframe to the empty dataframe with the highest of each type of seating type
  heighestData <- rbind(heighestData, filteredDataframe)
}

# displays the data frame
print(heighestData)


### Task 1 Q3 ######################################################################################

# creates a function with a parameter seating_type
displayComparison <- function(seating_type) {
  
  # creates a dataframe containing all the columns that are required
  data <- data.frame (
    Name = coasterData$name,
    Park = coasterData$park,
    seatingType = coasterData$seating_type,
    Speed = coasterData$speed,
    Status = coasterData$status
  )
  

  # creates a subset of the data for the rows are operating and have the same seating type as the parameter given
  data <- subset(data, data$seatingType == seating_type 
                 & data$Status == "status.operating")
  # orders the dataframe by speed, in descending order
  data <- data[order(-data$Speed),]
  
  # gets the top ten rows of the dataframe
  fastestRides <- head(data, 10)
  
  # creates a bar chart contains the speed and the roller coaster name
  barplot(fastestRides$Speed, # the speed of the roller coaster
          main = "Top 10 fastest rides", # bar chart name
          names.arg = fastestRides$Name, # a label for each value given
          xlab = "Ride name", # giving the horizontal axis a title
          ylab = "Speed", # gives the vertical axis a title
          las = 2) # rotates the name 90 degrees so it shows all the names
}

# runs the function with a given parameter
displayComparison("Inverted")


### Task 1 Q4 ######################################################################################

# creates a function with a parameter seating_type
correlateLengthSpeed <- function(seating_type) {
  
  # creates a dataframe containing all the columns that are required
  data <- data.frame (
    seatingType = coasterDataNoNA$seating_type,
    Height = coasterDataNoNA$height,
    Length = coasterDataNoNA$length,
    Speed = coasterDataNoNA$speed
  )
  
  # creates a subset of the data for the rows that have the same seating type as the parameter given
  data <- subset(data, data$seatingType == seating_type)

  # creates a dataframe containing information
  lengthSpeed <- data.frame (
    seatingType = seating_type, # the seating type provided by the parameter
    Height = mean(data$Height), # the average height of all roller coasters
    Length = mean(data$Length), # the average length of all roller coasters
    Cor = cor(data$Speed, data$Length) # the correlation between speed and length
  )
  
  # display dataframe
  print(lengthSpeed)
}

# runs the function with a given parameter
correlateLengthSpeed("Sit Down")


### Task 1 Q5 ######################################################################################

# creates a function
compareWoodenSteel <- function() {
  
  # creates a dataframe containing all the columns that are required
  data <- data.frame (
    materialType = coasterDataNoNA$material_type,
    Speed = coasterDataNoNA$speed
  )
  
  # creates a subset of the data for the rows that have material "steel" or "wooden"
  data <- subset(data, data$materialType == "Steel" | data$materialType == "Wooden")
  
  # creates a vector containing only speed values from steel roller coasters
  steelCoaster <- subset(data, data$materialType == "Steel")$Speed
  # creates a vector containing only speed values from wooden roller coasters
  woodenCoaster <- subset(data, data$materialType == "Wooden")$Speed
  
  
  # NULL hypotheses - there is no significant difference between the speeds of wooden and steel roller coasters
  # Alternative hypotheses - there is a significant difference between the speeds of wooden and steel roller coasters
  
  # performs the t test for 2 data sets with a two-tailed distribution and unequal variance
  testResult <- t.test(steelCoaster, woodenCoaster, alternative = "two.sided", var.equal = FALSE)
  
  # displays the result of the t test
  print(testResult)
  
  # conclusion
  # there is a significant difference between the speeds of wooden  and steel roller coasters
  # therefore we can reject the NULL hypothesis since 1.054e-12 < 0.05
}

compareWoodenSteel()
