# James Evans - 1910316
# Statistical Programming
# Assignment 1

#TASK 1

#1. Create a vector v1 that contains the values 1 to 16.

# Set up and display vector v1, using concatenate, which contains the values 1 to 16.

v1<-c(1:16)
v1



#2. Add the values 17 to 20 to the end of v1.

# Change and display vector v1, using concatenate, which will now contain the values 1 to 20.

v1<-c(v1,17:20)
v1



#3. Delete elements 5, 6, 7 and 13 from v1. You should use a one method to remove elements 5, 6, 7 and another method to remove element 13.

# To delete elements 5,6 and 7; first concatenate elements 1 to 4, and attach elements 8 to 20 to the right hand side.

v1<-c(v1[1:4],v1[8:20])
v1

# To then delete element 13 (which now has the value of 16; set individual elements as vector v1, excluding element 13, up to and including the final element (currently 17).

v1<-c(v1[1],v1[2],v1[3],v1[4],v1[5],v1[6],v1[7],v1[8],v1[9],v1[10],v1[11],v1[12],v1[14],v1[15],v1[16],v1[17])
v1


#4. Using v1, create a new vector v2 that contains the 1st to 5th and 9th to 11th elements of v1.

# Concatenate elements 1 to 5 with 9 to 11 from v1, and use these to create v2.

v2<-c(v1[1:5],v1[9:11])
v2


#5. Calculate v1*v2 and display the results. In your comments, explain how R adds two vectors of different lengths.

# Use the multiply operation (*) to multiply the new vectors and create a new vector ans1 to contain the answer. Display ans1.

ans1<-v1*v2
ans1

# When adding, or multiplying two vectors of differing lengths, R reaches the and off the shorter of the two vectors, then returns to the start of it, whilst continuing along the longer vector.
# I.e When adding a vector of length 16 another of length 8; element 9 of the longer vector is added/multiplied to/with element 1 of the shorter vector. Element 10 with element 2 and so on.


#6. Create a new vector v3 by selecting all even elements of v1 using a filter.

# Filter v1 so that all elements, which when modulated to 2 equal 0 (are even), are used to create v3.

v3<-v1[v1%%2==0]
v3


#7. Using a loop, create a new vector v4 the same length as v1, that contains the value TRUE if the corresponding element of v1 has an integer square root and the value FALSE otherwise. 
#HINT: You will need to use the sqrt() and round() functions.

#Create v4 and set it to NULL

v4<-NULL

# Main Loop
# Using seq to go through each element in v1.

for (i in seq(v1)){
  
  # Calculate square root of v1[i], check to see whether it is an integer, if it is return TRUE, otherwise return FALSE.
  #For each result; concatenate V4 as it stands, together with the latest result.
  
  v4<-c(v4,ifelse(sqrt(v1[i])==round(sqrt(v1[i]), digits=0), TRUE,FALSE))
  
}

#Display v4

v4


#8. Create a matrix m1 with 4 rows from vector v1.

# Use the matrix operation to create the matrix.
# Use v1 as the values to be included in the matrix.
# Set the number of columns as 4.

m1<-matrix(v1,nrow=4)
m1


#9. Create a matrix m2 with 2 columns from vector v2.

# Use the matrix operation to create the matrix.
# Use v2 as the values to be included in the matrix.
# Set the number of rows as 4=2.

m2<-matrix(v2,ncol=2)
m2



#10. Swap all of the elements in row 3 of m1 with the elements in column 2 of m1.

# Create a vector h1, which contains the elements within row 3 of m1.
h1<-m1[3,]

# Create a vector h1, which contains the elements within row 3 of m1.
h2<-m1[,2]

# Insert the vector h2 into row 3 of m1.
m1[3,]<-h2

# Display m1 following the initial replacement.
m1

# Insert the vector h1 into column 2 of m1.
m1[,2]<-h1

# This method ensures that the elements which are subsequently entered into the corresponding row/column is as it was at the starting position.
# The "swap" that has been requested in the question cannot be completed without overwriting a particular element.
# In this case, as we have first replaced row 3 with elements from column 2; then replacing column 2 overwrites one change initially made to row 3.
# In effect m1[3,2] changes from 10 to 9 and finally to 14.


# Display m1 following the subsequent replacement.
m1


#11. Create and display a new matrix m3 from m1 and m2 where m3 contains all of the columns of both source matrices.

# Use the column bind function to bind the matrix m2 to the right hand side of m1, and subsequently create m3.

m3<-cbind(m1,m2)

# Display m3.
m3


#12. Replace all values in m3 that are in (column 1 or column 3) and in (row 2 or row 3) with double their value.

# Using a loop throughout all rows of the matrix:-
#For each column within each row:-
#If column = 1, elements are replaced with double their value.
#If column = 3, elements are replaced with double their value.
#If row = 2, elements are replaced with double their value.
#If row = 3, elements are replaced with double their value.
#If none of the conditions above are satisfied; the initial value is brought back into the matrix.

for (i in seq(m3[,1])) {
  
  for(j in seq(m3[1,])){
    
    ifelse(j==1,m3[i,j]<-m3[i,j]*2,
           ifelse(j==3,m3[i,j]<-m3[i,j]*2,
                  ifelse(i==2,m3[i,j]<-m3[i,j]*2,
                         ifelse(i==3,m3[i,j]<-m3[i,j]*2,
                                m3[i,j]<-m3[i,j]
                         ))))
    
  }
}

#Display m3
m3


#13. Remove row 2 of m3.

# Use the "-" sign to drop the second row using reassignment as follows:-
m3<-m3[-2,]

#Display m3
m3





#14. Label the columns of m3 as "c1", "c2", "c3", "c4", "c5", and "c6".

#Use the colnames function to rename the columns:-

colnames(m3)<-c("c1", "c2", "c3", "c4", "c5","c6")

#Display m3
m3


#15. Create a vector mmeans that contains the means of each column.

#Use the apply function to generate the means of each column, and enter them into the new vector "mmeans".
#"Mean" is a function which exists in R already,m and so there is no need to create this first.

mmeans<-apply(m3,2,mean)

#Display mmeans
mmeans


#16. Create a barplot of mmeans.

#The barplot function is used, and the matrix "mmeans" is called to act as the values and x axis names.
#Colour the bars green.
#Label the x axis "Column".
#Label the y axis "Mean".


barplot(mmeans,
        col = "Green",
        xlab = "Column", 
        ylab = "Mean")



