#covidanalysis.r

covidDataCHN<-read.csv("Covid China.csv", header=TRUE, stringsAsFactors = FALSE)
covidDataUK<-read.csv("Covid UK.csv", header=TRUE, stringsAsFactors = FALSE)
#Read in the data files.
#To read in to R, the xlsx files first had to be saved as a .csv files

#Plot the number of cases and number of deaths by month for both countries

par(mar=c(5,5,3,5)) #neaten margin size
plot(as.numeric(covidDataCHN$total_cases), covidDataCHN$month, na.rm=TRUE, type="l", xlab="Date", ylab="Cases", main="Comparing Covid cases and deaths in China and the UK")

par(new = TRUE) #to plot a second graph on the same table
plot(as.numeric(covidDataCHN$total_deaths, covidDataCHN$month), type="l", yaxt="n", xaxt="n", xlab="", ylab="", col="orange") 

par(new=TRUE)
plot(as.numeric(covidDataUK$total_cases, covidDataUK$month), type="l", yaxt="n", xaxt="n", xlab="", ylab="", col="grey")

par(new = TRUE)
plot(as.numeric(covidDataUK$total_deaths,covidDataUK$month), type="l", xaxt="n", yaxt="n", xlab="", ylab="", col="blue")
axis(side=4)
mtext("Deaths", side=4)
legend("bottomright", c("Cases China","Deaths China", "Cases UK","Deaths UK"), col=c("Black","Orange","Blue","Grey"), lty=c(1,2))


#Create a data frame CovidSummary that shows descriptive statistics for the total number of new cases in China and the UK for April 20, October 20, April 21 and October 21. This should contain mean, standard deviation, median, max, min and range.

#Extracting the data for each month in each country
CHNApr20<-covidDataCHN$new_cases[71:100]
CHNOct20<-covidDataCHN$new_cases[255:284]
CHNApr21<-covidDataCHN$new_cases[436:465]
CHNOct21<-covidDataCHN$new_cases[620:649]
UKApr20<-covidDataUK$new_cases[62:91]
UKOct20<-covidDataUK$new_cases[246:275]
UKApr21<-covidDataUK$new_cases[427:456]
UKOct21<-covidDataUK$new_cases[611:640]

#Build the initial summary data frame
CovidSummary<-data.frame(CHNApr20, CHNOct20, CHNApr21, CHNOct21, UKApr20, UKOct20, UKApr21, UKOct21,stringsAsFactors=FALSE)

#for(i in 8)
#{
#  col<-mean(CovidSummary[,i])
#  meanResults<-c(meanResults, col)
#}
#meanResults
#Not sure why this didn't work. Tried a few different techniques.

#Generating means column by column
mean1<-mean(CovidSummary[,1])
mean2<-mean(CovidSummary[,2])
mean3<-mean(CovidSummary[,3])
mean4<-mean(CovidSummary[,4])
mean5<-mean(CovidSummary[,5])
mean6<-mean(CovidSummary[,6])
mean7<-mean(CovidSummary[,7])
mean8<-mean(CovidSummary[,8])
#concatenating results in to one vector
meanResults<-c(mean1,mean2,mean3,mean4,mean5,mean6,mean7,mean8)
#Add new vector to data frame and name it.
CovidSummary<-rbind(CovidSummary,meanResults)
row.names(CovidSummary[31,])<-"Mean"


#Generating standard deviation column by column
sd1<-sd(CovidSummary[,1])
sd2<-sd(CovidSummary[,2])
sd3<-sd(CovidSummary[,3])
sd4<-sd(CovidSummary[,4])
sd5<-sd(CovidSummary[,5])
sd6<-sd(CovidSummary[,6])
sd7<-sd(CovidSummary[,7])
sd8<-sd(CovidSummary[,8])
#concatenating results in to one vector
sdResults<-c(sd1,sd2,sd3,sd4,sd5,sd6,sd7,sd8)
#Add new vector to data frame and name it.
CovidSummary<-rbind(CovidSummary,sdResults)
row.names(CovidSummary[32,])<-"SD"


#Generating median column by column
med1<-median(CovidSummary[,1])
med2<-median(CovidSummary[,2])
med3<-median(CovidSummary[,3])
med4<-median(CovidSummary[,4])
med5<-median(CovidSummary[,5])
med6<-median(CovidSummary[,6])
med7<-median(CovidSummary[,7])
med8<-median(CovidSummary[,8])
#concatenating results in to one vector
medResults<-c(med1,med2,med3,med4,med5,med6,med7,med8)
#Add new vector to data frame and name it.
CovidSummary<-rbind(CovidSummary,medResults)
row.names(CovidSummary[33,])<-"Med"


#Generating max column by column
max1<-max(CovidSummary[,1])
max2<-max(CovidSummary[,2])
max3<-max(CovidSummary[,3])
max4<-max(CovidSummary[,4])
max5<-max(CovidSummary[,5])
max6<-max(CovidSummary[,6])
max7<-max(CovidSummary[,7])
max8<-max(CovidSummary[,8])
#concatenating results in to one vector
maxResults<-c(max1,max2,max3,max4,max5,max6,max7,max8)
#Add new vector to data frame and name it.
CovidSummary<-rbind(CovidSummary,maxResults)
row.names(CovidSummary[34,])<-"Max"


#Generating min column by column
min1<-min(CovidSummary[,1])
min2<-min(CovidSummary[,2])
min3<-min(CovidSummary[,3])
min4<-min(CovidSummary[,4])
min5<-min(CovidSummary[,5])
min6<-min(CovidSummary[,6])
min7<-min(CovidSummary[,7])
min8<-min(CovidSummary[,8])
#concatenating results in to one vector
minResults<-c(min1,min2,min3,min4,min5,min6,min7,min8)
#Add new vector to data frame and name it.
CovidSummary<-rbind(CovidSummary,minResults)
row.names(CovidSummary[35,])<-"Min"


#Generating range column by column
range1<-range(CovidSummary[,1])
range2<-range(CovidSummary[,2])
range3<-range(CovidSummary[,3])
range4<-range(CovidSummary[,4])
range5<-range(CovidSummary[,5])
range6<-range(CovidSummary[,6])
range7<-range(CovidSummary[,7])
range8<-range(CovidSummary[,8])
#concatenating results in to one vector
rangeResults<-c(range1,range2,range3,range4,range5,range6,range7,range8)
#Add new vector to data frame and name it.
CovidSummary<-rbind(CovidSummary,rangeResults)
row.names(CovidSummary[36,])<-"Range"
