# TASK 1 (Assignment 2)
# Theresa Darzins - Student No. 1909721

# Question 1

# climate.xls contains data on Swansea and Wuhan weather
# load these into data frames called climateData ensuring:
#  - all headers imported 
#  - cities imported as a factor
#  - script assumes data file stored in same folder as script file
#  - display the (2) resulting data frames

SwanClim<-read.csv("R Scripts/SwanClim.csv", header=TRUE)
# load Swansea
SwanClim
# display to check correctly imported

colnames(SwanClim[1])
# check columns 

colnames(SwanClim)[1]<-c("Measurements")
# rename column 1
colnames(SwanClim[1]) 
# check columns renamed

SwanClim 
# check matrix columns re-named

City<-rep("Swansea",7) 
# add Swansea id
City

SwanClim<-cbind(SwanClim[1],City,SwanClim[2:13]) 
# add Swansea onto matrix
SwanClim 
# check all labels right

# Repeat for Wuhan

WuhClim<-read.csv("R Scripts/WuhClim.csv", header=TRUE)
# load Wuhan
WuhClim

colnames(WuhClim[1]) 
# check matrix columns re-named

colnames(WuhClim)[1]<-c("Measurements") 
# rename columns
colnames(WuhClim[1]) 
# check renamed

WuhClim 
# check matrix columns named

City<-rep("Wuhan",7) 
# add Wuhan id
City

WuhClim<-cbind(WuhClim[1],City,WuhClim[2:13]) 
# add Wuhan onto matrix
WuhClim 
# check labels

climateData<-rbind(SwanClim,WuhClim) 
# join data frames
climateData

# Question 2

# Create a new data frame that shows the cumulative rainfall and
# cumulative hours of sunshine for each city.  Display in a data frame


SwanRain<-sum(climateData[4,3:14]) 
# Cumulative Swansea Rainfall
SwanRain

WuhanRain<-sum(climateData[11,3:14]) 
# Cumulative Wuhan Rainfall
WuhanRain

SwanSun<-sum(climateData[7,3:14]) 
# Cumulative Swansea Sunshine
SwanSun

WuhanSun<-sum(climateData[14,3:14]) 
# Cumulative Wuhan Sunshine
WuhanSun

Data<-c(SwanRain,WuhanRain,SwanSun,WuhanSun) 
# join Rainfall + Sunshine
Data

cumulativeData<-matrix(Data, nrow=2) 
# make into matrix
cumulativeData

colnames(cumulativeData) 
# add colnames
NULL
colnames(cumulativeData)<-c("Cum.Rainfall (mm)", "Cum.Sunshine (hrs)") 
# relabel column headers
cumulativeData

rownames(cumulativeData) 
# add row names
NULL
rownames(cumulativeData)<-c("Swansea", "Wuhan") 
# re-label rows
cumulativeData

swanRain<-c(climateData[4,3:14])
swanRain

m1<-matrix(swanRain, nrow=1) 
# make into a matrix
m1

colnames(m1)
NULL
colnames(m1)<-c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
m1

rownames(m1)
NULL
rownames(m1)<-c("Swansea Rain (mm)")
m1

wuhanHum<-c(climateData[12,3:14])
wuhanHum

m2<-matrix(wuhanHum, nrow=1)
m2

rownames(m2)
NULL
rownames(m2)<-c("Wuhan Humidity (%)")
m2

m3<-rbind(m1,m2) 
# join data frames
m3

# Question 3

# Include a function display Graph()that will take the city name,weather
# parameter over the year for that city.  Display this using humidity
# in Wuhan and rainfall in Swansea

#plot(
#  pch="+" # shows points as "+"
#)

climateData
climateData[4,3:14]
climateData[12,3:14]

plot(c(1,12),c(0,160))
x<-c(1,2,3,4,5,6,7,8,9,10,11,12)
y<-c(climateData[4,3:14])

# Rainfall Swansea plot

plot(
  x,y,
  xlab="Months",
  ylab="Value",
  type="l", 
  # joins lines,
  main="Yearly Weather",
  col="green"
)

legend(
  "top", 
  c("Swansea Rainfall (mm)"),
  fill=c("green")
)

# Humidity Wuhan plot

plot(c(1,12),c(0,1))
x<-c(1,2,3,4,5,6,7,8,9,10,11,12)
z<-c(climateData[12,3:14])

plot(
  x,z,
  xlab="Months",
  ylab="Value",
  type="l", 
  # joins lines,
  main="Yearly Weather",
  col="red"
)
legend(
  "topleft", 
  c("Wuhan Humidity (%)"),
  fill=c("red")
)

# Question 4

# Include a function compareCities()that will take start month and end month and
# 2 weather parameters, and plots the data for those parameters for both cities 
# on the same graph.  
# Bar Charts Chosen.  
# Appropriate labelling and colours should be used

swanMaxT<-c(SwanClim[3,])
# Swansea Max Temp - 12 months 
swanMaxT
wuhanMaxT<-c(WuhClim[3,]) 
# Wuhan Max Temp - 12 months 
wuhanMaxT
swanMinT<-c(SwanClim[2,]) 
# Swansea Min Temp hrs - 12 months 
swanMinT
wuhanMinT<-c(WuhClim[2,]) 
# Wuhan Min Temp hrs - 12 months
wuhanMinT

compareCities<-rbind(swanMaxT,wuhanMaxT,swanMinT, wuhanMinT)
# binds rows sunshine hrs for both cities
compareCities

colnames(compareCities)<-c("Weather", "City", "Jan","Feb", "Mar","Apr","May",
                           "Jun","Jul","Aug","Sep","Oct","Nov","Dec")
# rename column 1
colnames(compareCities)
# check renamed
compareCities

barplot(
  compareCities[,3:14],
  main = 
    "Max and Min Temperature
Monthly Weather Comparisons 
(Swansea vs Wuhan)",
  ylim=c(0,100),
  xlab="Month",
  ylab= "Degrees Centigrade",
  col = c("pink","red","green","blue")
)
legend(
  "topright",c("MaxTempSwansea","MaxTempWuhan", 
               "MinTempSwansea","MinTempWuhan"),
  fill = c("pink","red","green","blue")
)








