### Function to calculate area of petals ###
calculateArea <- function(IrisData)
{
  area<- NULL
  #loop to check the species of petal and then carries out the appropriate area formula
  for (i in seq(1:length(IrisData$Species))){
    if (IrisData[i,"Species"]=="Iris-setosa"){
      calcArea = IrisData[i,"Petal.width"] * IrisData[i, "Petal.length"] * 0.8
      area <- c(area, calcArea) #concatonates the result into the area vector
    }
    if (IrisData[i,"Species"]=="Iris-versicolor"){
      calcArea = IrisData[i,"Petal.width"] * IrisData[i, "Petal.length"] * 0.75
      area <- c(area, calcArea)
    }
    if (IrisData[i,"Species"]=="Iris-virginica"){
      calcArea = IrisData[i,"Petal.width"] * IrisData[i, "Petal.length"] * 0.65
      area <- c(area, calcArea)
    }
  }
  return(area) #returns the vector area which  contains the areas of all the petals
}
### Main Script ###
#load the csv file into a data frame
IrisData<-read.csv("iris.csv", header=TRUE, stringsAsFactors = TRUE)
SetosaCount = 0;
VersicolorCount = 0;
VirginicaCount = 0;
#loop to check the species and increment the appropriate count
for (i in seq(1:length(IrisData$Species))){
  if (IrisData[i,"Species"]=="Iris-setosa"){
    SetosaCount = SetosaCount + 1;
  }
  if (IrisData[i,"Species"]=="Iris-versicolor"){
    VersicolorCount = VersicolorCount + 1;
  }
  if (IrisData[i,"Species"]=="Iris-virginica"){
    VirginicaCount = VirginicaCount + 1;
  }
}
#Takes the species counts and records them into a new data frame
IrisSummary <- data.frame("Species" = c("Iris-setosa", 
                                        "Iris-versicolor", 
                                        "Iris-virginica"), 
                          "Amount" = c(SetosaCount, 
                                       VersicolorCount, 
                                       VirginicaCount))
#Calculates the pearson correlation between the sepal and petal length
lengthCor <- cor(IrisData$Sepal.length, 
                 IrisData$Petal.length, 
                 method ="pearson")
#Plots a scattergraph for the sepal and petal length
plot(x=IrisData$Sepal.length, 
     y=IrisData$Petal.length, 
     xlab="Sepal Length", 
     ylab="Petal Length",
     main="Sepal Length Vs. Petal Length")
#creates a new column for the petal area and fills the rows with the area vector calculated from the calculateArea Function
IrisData["Petal Area"] <- calculateArea(IrisData)
