#1704446 - Christine Woods





#irisdata loaded into a data frame with headings making sure the species
#names are imported as a factor

irisdata<-read.csv("iris.csv", header=TRUE,stringsAsFactors = TRUE)                
irisdata



#new data frame set up with the species column called iris summary
#which is then printed out to show the data frame
 
irissummary<-data.frame(irisdata$Species)                                         
print(irissummary)



# Counters set up to use in the loop to count each of the species.    
# For loop completed to run through the sequence of every row of the dataset
# depending on the name of the species, incrementing each counter as appropriate

countSetosa<-0
countVersicolor<-0
countVirginica<-0

for(i in seq(irisdata$Species))                                                  
{
 
  if(irisdata$Species[i]=="Iris-setosa") countSetosa<-countSetosa+1              
  if(irisdata$Species[i]=="Iris-versicolor") countVersicolor<-countVersicolor+1
  if(irisdata$Species[i]=="Iris-virginica") countVirginica<-countVirginica+1
}



#display the amount of each species in the data frame

countSetosa
countVersicolor
countVirginica



#The Pearson correlation method is used between the petallength and the sepal length, 
#to show the correlation between the Petal length and the Sepal length, the result = 0.8717542 
#this shows the correlation between the two variable (petal length and sepal length)
#is apositive correlation as it is between 0-1.if it had been between 0 and -1 there 
#would be negative correlation

cor(irisdata$Petal.length,irisdata$Sepal.length,method = "pearson")      




#Using the plot instruction to plot a scatterplot of the petal length and sepal length, 
#both axes are labelled also providing a plot title and changing the colour to red

Petal_length = (irisdata$Petal.length)                                                          
Sepal_length = (irisdata$Sepal.length)                                                          
plot(Petal_length,Sepal_length,main="Iris Analysis, Petal Length vs Sepal Length", col ="Red")  
                                                                                               


#Using a function called calculateArea() which is applied to the irisData data frame, a new column
#has been added to the irisData that shows the leaf area for each leaf making sure it is extracted
#as numeric otherwise it would be a character as the first vector in the data frame is also a character.
#The return areavalue must be included for the code work.

calculateArea<-function(x)                                                                       
  
{
  Petal.length<-as.numeric(x[3])
  Petal.width<-as.numeric(x[4])
  
  if (x[5]=="Iris-setosa") areavalue<-Petal.length*Petal.width*0.8                              
  if (x[5]=="Iris-versicolor") areavalue<-Petal.length*Petal.width*0.75                          
  if (x[5]=="Iris-virginica") areavalue<-Petal.length*Petal.width*0.65                            

return(areavalue)
                                                                                                  
}



#leafarea vector is made in which to put the newly calculated data by applying it to the
#data frame

leafarea<-apply(irisdata,1,calculateArea)                                                         
leafarea                                                                                           



#the leafarea column is put into the irisdata set using cbind.  The column is now
#in the iris data set

irisdata<-cbind(irisdata,Leaf_area=leafarea)                                                      
                                                                                                 







