# TASK 1 (50%) - ASHLEIGH JOSEPH SIMONS - 265050

# 1. ---------------------------------------------------

# Creating v1 and giving it the numbers 1 to 16. 

v1<-seq(1, 16, by=1) 

v1 # display elements


# 2. ---------------------------------------------------

# Within newValues will be the numbers 17 to 20.

newValues<-seq(17, 20, by=1) 
newValues 


# Now they can be added to the end of v1. 

v1<-c(v1[1:16], newValues)

v1 # display elements


# 3. ---------------------------------------------------

# First we will delete 5, 6, 7 from v1.

v1<-c(v1[1:4], v1[8:20])

v1 # display elements


# Now that we have 17 numbers left, 13 is in element 10 of 1 to 17
# so this is the element that will be removed.

v1<-c(v1[1:9], v1[11:17])

v1 # display elements


# 4. ---------------------------------------------------

# We need to create v2 and then use the current elements in v1 to give
# the 1st to 5th and 9th to 11th elements to v2. 

v2<-c(v1[1:5], v1[9:11])

v2 # display elements


# 5. ----------------------------------------------------

# Now v1 and v2 will be multiplied within vector results.

results<-v1*v2


# As v1 and v2 are of different lengths, v1 having  16 elements and 
# v2 having 8. The remaining 8 elements that do not have a element to 
# multiply in v1 use v2 and reiterate the required amount of  
#times (8) to give those elements a multiplication result. 

results # display elements


# 6. ----------------------------------------------------

# v3 is the new vector the elements need to go into. So we can put all the 
# elements from v1 using <-v1 and using '[]' we can add specifically what
# is being searched for being being added. So in this case it needs to be even
# or in code divisible by 2 with no remainders, making it even. Once the 
# element is confirmed even it is added.

v3<-v1[v1%%2==0]

v3 # display elements


# 7. ----------------------------------------------------

# To be able to create vector v4 i am going to use v1 from the previous 
# questions in this loop

for (i in v1)
{
  # Firstly we need the square roots of the numbers in v1, however they then
  # have a number of decimal places that we don't want.
  
  roots<-c(sqrt(v1))
  
  roots # display elements
  
  # Now we can use round() to make the decimal places 0 and the numbers will 
  # be rounded accordingly to 0 decimal places
  
  test1<-round(sqrt(v1), digits = 0)
  
  test1 # display elements
  
  
  # Now we can use the vector v4 to store the values "TRUE" and "FALSE" 
  # depending on if the values in v1 divide by the square roots with 0 
  # remainders
  
  v4<-c(ifelse(v1%%test1==0,"TRUE","FALSE"))
}

v4 # display elements


# 8. ----------------------------------------------------

# m1 is the first matrix and i will use the matrix() function to create it
# then using v1 within the matrix as these are the elements that need to be 
# shown. the use of 'nrow =' makes the rows a specific amount depending on 
# what is entered after.In this case its 4.

m1<-matrix(c(v1), nrow = 4)

m1 # display matrix m1


# 9. ----------------------------------------------------

# For m2 the same process is used as m1 but the vector name changes, v2 is 
# used instead of v1 and instead of having 'nrow =', 'ncol =' is used to 
# select the amount of columns required. In this case it is 2.

m2<-matrix(c(v2), ncol = 2)

m2 # display matrix m2


# 10. ---------------------------------------------------

# Now m1 has to be changed so similar to the first two parts but instead of 
# only using v1 or v2, both are used. Going along selecting the elements 
# rather than putting them all in at once. the 5th, 6th, 7th and 8th elements
# in v2 needed to be placed in the places of elements 3, 7, 11, and 15. 
# Finally, 'nrow = 4' put these elements into 4 rows in the matrix.

m1<-matrix(c(v1[1:2], v2[5], v1[4:6], v2[6],
            v1[8:10], v2[7], v1[12:14], v2[8],
            v1[16]), nrow = 4)
m1 # display matrix m1


# Similarly to the previous section of code the matrix now requires v1 and v2
# elements. The first 4 elements are from v2 so they can be first and then
# elements 3, 7, 11, and 15 can be added after. 'ncol = 2' is used then to 
# put these elements into two columns in the matrix.

m2<-matrix(c(v2[1:4], v1[3], v1[7], v1[11], v1[15]), ncol = 2)

m2 # display matrix m2


# 11. ---------------------------------------------------

# To create a new matrix m3, m1 and m2 can be combined to give one vector
# containing all the elements. This vector will be called m. 

m<-c(m1,m2)

m # display elements


# Now m can be used to put all of the elements into the matrix m3 with
# 6 columns.

m3<-matrix(m, ncol = 6)

m3 # display matrix m3


# 12. ---------------------------------------------------

# To replace all of the values in m3 that are in columns 1 and 3 and 
# rows 2 and 3 there will need to be some sub matrices.

# This first sub matrix selects all of the values in column 1 and times them
# by two.

sub1m3<-matrix(c(m3[,1]) * 2)

sub1m3 # display sub matrix 1 of m3: sub1m3.


# This second sub matrix selects all of the values in column 3 and times them
# by two.

sub2m3<-matrix(c(m3[,3]) * 2)

sub2m3 # display sub matrix 2 of m3: sub2m3.


# This third sub matrix selects all of the values in row 2 and times them
# by two.

sub3m3<-matrix(c(m3[2,]) * 2)

sub3m3 # display sub matrix 3 of m3: sub3m3.


# The fourth sub matrix selects all of the values in row 3 and times them
# by two.

sub4m3<-matrix(c(m3[3,]) * 2)

sub4m3 # display sub matrix 4 of m3: sub4m3.


# In the final sub matrix it contains all of the values called out of 
# the individual previous four sub matrices and puts the values in order 
# of the values that they were before some were doubled. 

sub5m3<-matrix(c(sub1m3[1], sub1m3[2], sub1m3[3], sub1m3[4], m[5], 
                 sub3m3[2], sub4m3[2], m[8], 
                 sub2m3[1], sub2m3[2], sub2m3[3], sub2m3[4], m[13], 
                 sub3m3[4], sub4m3[4], m[16], m[17], 
                 sub3m3[5], sub4m3[5], m[20], m[21], 
                 sub3m3[6], sub4m3[6], m[24]))


# This is now the m3 matrix that has all of the values in it with the values
# required to be doubled.

m3<-matrix(sub5m3, ncol = 6)

m3 # display the m3 matrix


# 13. ---------------------------------------------------

# First we will use the previous code to create matrix m3

m3<-matrix(sub5m3, ncol = 6)

m3 # display the m3 matrix

# Now we can remove column 2 from m3 using m3[-2,]. This will remove all of 
# the values that was in that column.

m3<-m3[-2,]

m3# display the m3 matrix


# 14. ---------------------------------------------------

# To be able to replace the column names we need to use the previous
# code first

m3<-matrix(sub5m3, ncol = 6)
m3<-m3[-2,]

# Now we can use 'colname()' to assign each column a new name, in this case
# they are c1-c6

colnames(m3)<-c("c1", "c2", "c3", "c4", "c5", "c6")
m3 # display the m3 matrix with column names


# 15. ---------------------------------------------------

# To create a vector mmeans that contains the means of each column the
# previous code can be used so that we can display the matrix as it
# is what we need to use.

m3<-matrix(sub5m3, ncol = 6)
m3<-m3[-2,]
colnames(m3)<-c("c1", "c2", "c3", "c4", "c5", "c6")
m3 # display the m3 matrix with column names


# Now mmeans can be created so that the values, after the calculation, 
# have a place to be kept. NULL is used as place values as it won't be shown
# after the calculation and it is good for undefined values

mmeans<-c(NULL, NULL, NULL, NULL, NULL, NULL)


# The loop can be created using the column names in m3 'colnames(m3)', as 
# this is the amount of times we require the loop.

for (i in colnames(m3))
{
  mmeans[i]<-(mean(m3[,i])) # Sets the number of times the loop has been 
                            # performed as the position in mmeans and puts
                            # the mean of the column in m3 as the value in 
                            # mmeans.
}

mmeans # display the mmeans values 


# 16. ---------------------------------------------------

# To create the bar plot of mmeans we just need to use the previous vector
# mmeans and use the bar plot function. ' main = "" ' can be used to give a 
# title to the bar plot and '  xlab = "" ' can be used to give the data context

barplot(mmeans, main = "Means of Values in m3", xlab = "Column")

