# Read in the csv file and make the strings column a factor
irisData<-read.csv("iris.csv", header=TRUE, stringsAsFactors = TRUE)
irisData
summary(irisData)
#Create function to loop through the data to get the counts
generateSummary<-function(x)
{
# Create vector with the Species in
species<-c("Iris-setosa","Iris-versicolor","Iris-virginica")
species
# Create vector for the count
counts<-c(0,0,0)
counts
# Create the df frame the loop will add to
irisSummary<-data.frame(Species=species,Num_Records = counts)
irisSummary
# Create the loop to get the counts
#for each row of our data frame...
for (i in seq(1:length(x$Species)))
{
# For each row of the summary table...
for(j in 1:3)
{
# ... check to see which row of the summary table it refers to
if (x[i,5]==irisSummary[j,1])
{
# If the names match, increment the relevant counter
irisSummary[j,2]<-irisSummary[j,2]+1
}
}
}
# Return the completed summary data frame to the main script
return(irisSummary)
}
# Run the function on IrisData to see the counts
generateSummary(irisData)
# Create table to calculate the Pearson Correlation between the petal length and the sepal length
lengthData<-data.frame(PetalLength = irisData$Petal.length, SepalLength =irisData$Sepal.length)
lengthData
cor(lengthData,method="pearson")
# Create a scatterplot of the petal length and sepal length
plot(x=lengthData$PetalLength, y=lengthData$SepalLength, xlab="Petal Length",ylab="Sepal Length",
main="Petal Length vs. Sepal Length")
# Create a function to calculate the area of each record
calculateArea<-function(x)
{
PetalArea<-NULL
for (i in seq(1:length(x)))
{
if (x[i,5]=="Iris-setosa")
{
PetalArea[i] <- x[i,4] * x[i,3]*0.8
}
if (x[i,5]=="Iris-versicolor")
{
PetalArea[i] <- x[i,4] * x[i,3]*0.75
}
if (x[i,5]=="Iris-virginica")
{
PetalArea[i] <- x[i,4] * x[i,3]*0.65
}
}
x<- cbind(x,PetalArea)
return(x)
}
# Run the function on IrisData to include the Petal Area as a field on the dataset
calculateArea(irisData)
irisSummary
# Read in the csv file
kickData<-read.csv("kicks.csv", header=TRUE)
kickData
summary(kickData)
# Create function to calculate the descriptive statistics
generateStats<-function(x)
{
# Create empty vector to add values to
xStats<-NULL
# Calculate the statistics we want
xStats<-c(xStats, mean(x, na.rm=TRUE))
xStats<-c(xStats, median(x, na.rm=TRUE))
xStats<-c(xStats, max(x, na.rm=TRUE))
xStats<-c(xStats, min(x, na.rm=TRUE))
xStats<-c(xStats, sd(x, na.rm=TRUE))
# This Calculates the range
xRange<-range(x, na.rm=TRUE)
xStats<-c(xStats, xRange[2]-xRange[1])
# Return our summary table at the end
return(xStats)
}
# Create labels for our data frame which inclues the descriptive statistics
StatsLabels<-c("Mean", "Median", "Max", "Min", "Standard Deviation", "Range")
# Calculate descriptive statistics for each type of filled balls, using function created above
StatsAF<-generateStats(kickData$Air)
StatsHF<-generateStats(kickData$Helium)
# Create data frame from labels, $Air and $Helium data
kickSummary<-data.frame(Labels=StatsLabels,AirFilled=StatsAF, HeliumFilled=StatsHF)
kickSummary
# Create vectors for the two different samples
SampleAir<-kickData$Air
SampleHelium<-kickData$Helium
# Use Shapiro test to see if data is normal for both air filled and helium filled samples
shapiro.test(SampleAir) #p-value > 0.05 and so normally distributed
shapiro.test(SampleHelium) #p-value < 0.05 and so not normally distributed
# Create boxplot to show outliers for the Helium Filled samples and see if any for the Air
boxplot(SampleHelium, main="Helium")
boxplot(SampleAir, main="Air")
# Looks like there is one outlier for Air at 15/16 so filter this out of the data
SampleAir<-SampleAir[SampleAir > 16]
boxplot(SampleAir, main="Air")
# Check still normally distributed
shapiro.test(SampleAir) #p-value still > 0.05 and so normally distributed
# Outliers look like they are above 35 and below 20 for Helium so remove these from the data sample. Remove lower end first
SampleHelium<-SampleHelium[SampleHelium>= 20]
boxplot(SampleHelium, main="Helium")
# Remove top end outliers
SampleHelium<-SampleHelium[SampleHelium<= 35]
boxplot(SampleHelium, main="Helium")
# Check if normally distributed
shapiro.test(SampleHelium) #p-value now > 0.05 so normally distributed
# Perform t-test on the normally distributed samples
# H0: There is no difference between the distances travelled, i.e. both air filled and helium filled travel the same distance
# H1: There is a difference between the distances travelled, i.e. the ball filled with one type travels further than another
# Perform the t-test
t.test(SampleHelium, SampleAir)
# The calculated p-value for this is 0.09543. This is more than 0.05,
# this means we cannot reject the null hypothesis at the p=0.05 level.
# Therefore we accept the null hypothesis that the type of air that fills the ball has no difference on the distance travelled
# Create function which assigns all odd numbered rows to one vector and all even numbered rows to another
identifypararity<-function(y)
{
#Create empty vectors to put the even and odd row data in
evenKicks<-NULL
oddKicks<-NULL
for (i in seq(1:length(y[,1])))
{
if (y[i,1] %% 2 == 0) # checks if the row number is even and then outputs the data
{
evenKicks<-c(evenKicks,y[i,2])
evenKicks<-c(evenKicks,y[i,3])
}
if (y[i,1] %% 2 != 0) # checks if the row number is odd and then outputs the data
{
oddKicks<-c(oddKicks,y[i,2])
oddKicks<-c(oddKicks,y[i,3])
}
}
# Create summary table which assigns odd kicks to Neil and even to Jonny
kickersummary<-data.frame(Neil = oddKicks,Jonny =evenKicks)
return(kickersummary)
}
# Run function on kickData
kickerData<-identifypararity(kickData)
JonnySample<-kickerData$Jonny
NeilSample<-kickerData$Neil
# Check that these are normally distributed using Shapiro test
shapiro.test(NeilSample)#p-value > 0.05 so normally distributed
shapiro.test(JonnySample)#p-value < 0.05 so not normally dsitributed
# Check if there are any outliers for Neil
boxplot(NeilSample, main="Neil")
# Remove the one outlier
NeilSample<-NeilSample[NeilSample>15]
boxplot(NeilSample, main="Neil")
# Iterate filter again
NeilSample<-NeilSample[NeilSample<35]
boxplot(NeilSample, main="Neil")
# Check data still normal
shapiro.test(NeilSample) #p-value > 0.05 so normally distributed
# Look for outliers
boxplot(JonnySample, main="Jonny")
# Outliers found so filter out samples < 18
JonnySample<-JonnySample[JonnySample>=18]
boxplot(JonnySample, main="Jonny")
# Still have outliers so iterate again
JonnySample<-JonnySample[JonnySample>21]
boxplot(JonnySample, main="Jonny")
JonnySample<-JonnySample[JonnySample<32]
boxplot(JonnySample, main="Jonny")
# No more outliers for check normal distribution again
shapiro.test(JonnySample)#p-value > 0.05 so normally dsitributed
# Now we can perform the t-test
# Ho:There is no difference between the two kickers
# H1: There is a significant difference between the two kickers
t.test(JonnySample, NeilSample)
# The calculated p-value for this is 0.3667. This is more than 0.05,
# this means we cannot reject the null hypothesis at the p=0.05 level.
# Therefore we accept the null hypothesis that the kicker has no difference on the distance travelled
View(kickSummary)
