-------------------------------
Title: "Assignment 2 Task 2"
Student: "Emily Stockdale"
Studentnumber: "1906962"
-------------------------------

#package used, it allows for cleaner code and to simplify data manipulation Q3&Q4
#evidence of research/further reading- https://www.rstudio.com/blog/introducing-dplyr/, https://afit-r.github.io/dplyr, https://datacarpentry.org/R-genomics/04-dplyr.html
  
  library(dplyr) #load

## Main Script ##
#Q1

#read in data frame's using read.csv 
#the data is held in 2 files
#covidchina.csv
#coviduk.csv
  
#Read in China Covid data, including strings as factors this will allow for analysis later on.
  
coviddatachina<-read.csv("covidchina.csv", header=TRUE, stringsAsFactors = TRUE)

#Read in UK Covid data

coviddatauk<-read.csv("coviduk.csv", header=TRUE, stringsAsFactors = TRUE)

#display Covid data China

coviddatachina

#display Covid data UK

coviddatauk

#merge the data frames using Rbind 

coviddata<-rbind(coviddatachina,coviddatauk)

#Display Covid data

coviddata

#Q2  

#plot the number of Covid19 cases and number of deaths by month for both countries

#neaten margin size

par(mar=c(5,5,3,5))

#Plot the data from the data frame for the graph, adding a main title 

plot(as.numeric(coviddatachina[,6], coviddatachina[,7]), na.rm=TRUE, type="l", xlab="Days Since Beginning of Pandemic",
     ylab="Cases", main="Comparing COVID19 Cases and Death Rate in China and the UK")

#par used to plot a second graph on the same table

par(new=TRUE)
plot(as.numeric(coviddatachina[,9], coviddatachina[,10]), type="l", yaxt="n",xaxt="n", xlab="", ylab="", col="pink")


par(new=TRUE)
plot(as.numeric(coviddatauk[,6], coviddatauk[,7]), type="l", yaxt="n",xaxt="n", xlab="", ylab="", col="purple")


par(new=TRUE)
plot(as.numeric(coviddatauk[,9], coviddatauk[,10]), type="l", yaxt="n", xaxt="n", xlab="", ylab="", col="orange")

axis(side=4)
mtext("Deaths", side=4)

#insert a legend to appropriately label the graph

legend("bottomright", c("Cases China", "Deaths China", "Cases UK", "Deaths UK"), 
       col=c("black", "pink","purple","orange"), lty=c(1,2))

##left axis formatting is wrong - tried changing excel from scientific but axis remained the same
##graph is displaying days since pandemic start rather than specifying dates (should be displaying month of pandemic for 2020 & 2021)

#Q3
#Assign data to CovidSummary from data frame coviddata
#Months April 20 & 21, Oct 20 & 21
#CovidSummary will display new case data from China and the UK from coviddata data frame

CovidSummary <-  coviddata[coviddata$month %in% c("Apr-20", "Apr-21", "Oct-20", "Oct-21"), ]
CovidSummary <- CovidSummary[ , c("location","month", "new_cases")]

#identify mean, standard deviation, median, max, min, and range & assign to CovidSummary.
# %>% used to chain together functions
#could use a nested loop however chaining is used for 'cleaner' code
CovidSummary<-CovidSummary %>%                               
  group_by(month, location) %>% 
  summarize(min = min(new_cases),
            median = median(new_cases),
            mean = mean(new_cases),
            max = max(new_cases),
            std.d = sd(new_cases),
            #range = range(new_cases)  ### Not working
  )

#the range function wasn't working in the same data frame
#added as a column
#add column 'range' to CovidSummary

CovidSummary$range <-paste0(as.character(CovidSummary$min)," - ", as.character(CovidSummary$max))

#Display CovidSummary

CovidSummary


#Q4
##question 4 incomplete

#is there a significant difference in the numbers of new cases in the UK between Oct 20 & Oct 21

#filter for Oct data for UK cases
#Filter for Oct Data
TCovidSummary <-  coviddata[coviddata$month %in% c("Oct-20", "Oct-21"), ]

#Filter for Location (UK)

TCovidSummary <-  TCovidSummary[TCovidSummary$location %in% c("United Kingdom"), ]

#two sample t test 

t.test(TCovidSummary$new_cases  ~  TCovidSummary$month  ,var.equal=TRUE)

#p-value of 0.00000000000000022 so the test can reject the null hypothesis with a high degree of significance

