

# Assignment 2 - Due date: 17/07/20
# Submission Date: 03/05/20
# Lecturer: Dr Nik Whitehead
# Module: Statistical Programming
# University of Wales Trinity St David

# Student No: 1707800
# Student Name: Mike Wakelyn



# Task 1 - IrisAnalysis

# Set working directory.  To be commented out before submitting
# setwd("C:/Users/mi001511/Desktop/Uni Software/01 - Stat Prog/Assignment/Assignment 2/Iris")


# --------------------------------------------------------------------------------------------


# ----------- Funcitons Start -----------

calculateArea<-function(x){
  
  petLength<-as.numeric(x[3]) # Access element 3 of irsiData
  petWidth<-as.numeric(x[4])  # Access element 4 of irsiData
  petSpecies<-x[5]            # Access element 5 of irisData
  
  
  if (petSpecies=="Iris-setosa")# Calculate area for Iris-setosa
  {
    petArea<-petWidth*petLength*0.8
  }
  else if (petSpecies=="Iris-versicolor") # Calculate area for Iris-versicolor
  {
    petArea<-petWidth*petLength*0.75
  }
  else # Calculate area for Iris-virginica
  {
    petArea<-petWidth*petLength*0.65
  }
  
  return(petArea)
}

# ----------- Funcitons End -----------


# --------------------------------------------------------------------------------------------


# Task 1.1

# Load iris.csv to the dataframe called IrisData.  Include headers and set strings to factors
irisData<-read.csv("iris.csv", header=TRUE, stringsAsFactors = TRUE)

# Display irisData dataframe
irisData


# --------------------------------------------------------------------------------------------


#Task 1.2

# Set up a new dataframe with a column for Species and a column for Number.
number<-c(0,0,0)  # Set Number (initial count per species) to 0 
species<-c("Iris-setosa","Iris-versicolor","Iris-virginica")  # Add species names in Species column
irisSummary<-data.frame(Species=species,Number=number) # Make data frame

# Display irisSummary
irisSummary


# FOR loop to count the number of each species and add to the irisSummary dataframe
for (i in 1:nrow(irisData))
{
  # Identify species name and count per species
  species<-irisData[i,"Species"]
  number<-irisData[i,"Number"]
  
  # Increment the total for each species in the irisSummary data frame
  # Use three IF statements to target each species
  # If the statement is true (ie. the species name exists)
  # Take the existing value of the Numbers column for the species row (initially set to 0)
  # Increase the value by 1
  # Store in irisSummary$Numbers against the appropriate species name
  
  if (irisData[i,"Species"]=="Iris-setosa")# Update the count of species for Iris-setosa
  {
    irisSummary$Number[1]<-irisSummary$Number[1]+1 
  }
  if (irisData[i,"Species"]=="Iris-versicolor") # Update the count of species for Iris-versicolor
  {
    irisSummary$Number[2]<-irisSummary$Number[2]+1 
  }
  if (irisData[i,"Species"]=="Iris-virginica") # Update the count of species for Iris-virginica
  {
    irisSummary$Number[3]<-irisSummary$Number[3]+1 
  }
  
} # End of FOR loop

# Display IrisSummary
irisSummary

# The above can also acheived using the table() function
# Creating the irisSummary data frame by using the table() function
# to count the number of species as follow:

# irisSummary<-as.data.frame(table(irisData$Species))

# This returns:

# > irisSummary
#             Var1 Freq
# 1     Iris-setosa   50
# 2 Iris-versicolor   50
# 3  Iris-virginica   50

# The data frame column names can then be changed using:

# colnames(irisSummary)<-c('Species', 'Number')
# irisSummary

# This returns

# > irisSummary
#           Species Number
# 1     Iris-setosa     50
# 2 Iris-versicolor     50
# 3  Iris-virginica     50


# --------------------------------------------------------------------------------------------


# Task 1.3

# Calculating the Pearson correlation between petal length and sepal length

# Create a new data frame with irisData$Sepel.length and irisData$Petal.length columns
# This is to enable the use of the cor() function as it needs to be fed a data frame
# rather than vectors
pearsonCor<-data.frame(SepLength=irisData$Sepal.length, petLength=irisData$Petal.length)

# Use the cor() function to apply the Pearson correlation to the data frame
cor(pearsonCor, method="pearson")


# --------------------------------------------------------------------------------------------


# Task 1.4


# Scatter plot of Sepal Length vs. Petal Length
# Plotting the sepal length on the x axis and petal length on the y axis
# Labelling the axis respectively and adding an overhead title
plot(x=pearsonCor$SepLength, y=pearsonCor$petLength, xlab="Sepal Length", ylab="Petal Length", main="Sepal Length vs. Petal Length")


# --------------------------------------------------------------------------------------------


# Task 1.5


# Apply() the calculateArea() function to each row of irisData and store in Petal.area
Petal.area<-apply(irisData, 1, calculateArea)

# Display Petal.area
Petal.area

# Bind Petal.area to irisData as a new column
irisData<-cbind(irisData, Petal.area)

#Display irisData
irisData


# End -----------------------------------------------------------------------------------------

