#Assignment 1, Task 1
#Emily Stockdale, 1906962

#Q1 create a vector v1 that contains the values 1 to 16.

#create vector v1
v1 <- c(1:16)

v1 #display v1


#Q2 Add the values 17 to 20 to the end of v1.

v1 <- append(v1,17:20)

v1 #display v1


#Q3 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.

v1 <-v1[-5:-7] #delete elements 5,6,7.

#display v1
v1

#alternative method to remove 13 

v1 <- v1[ v1 != 13 ]

#display v1
v1


#Q4 Using v1, create a new vector v2 that contains the 1st to 5th and 9th to 11th elements of v1.

#create v2
v2 <- c(1,2,3,4,8,12,14,15)

#display v2
v2


#Q5 Calculate v1*v2 and display the results. This will generate a warning; in your comments,explain how R adds two vectors of different lengths.
 
v1*v2

#the vector will recycle until it matches the larger vector, it will continue multiplying.


#Q6 Create a new vector v3 by selecting all even elements of v1 using a filter.

#the which object allows to select indicies from a referenced array.
v3 <- v1[which(v1 %%2==0)] 

v3 #display v3


#Q7 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.

v4 <- NULL #set up vector to be looped through

v4 #display v4

for (j in v1) { #obtains values from v1
  v4 <- sqrt(v1) #v4 with the square rooted results from v1
  v4 <- v4 ==round(v4,0) 
}
v4 #display v4


#Q8 Create a matrix m1 with 4 rows from vector v1.

#create m1
m1 <- matrix(v1, nrow=4, byrow=T)

#display m1
m1


#Q9 Create a matrix m2 with 2 columns from vector v2.

#create m2
m2 <- matrix(v2, ncol=2, byrow=T)

#display m2
m2


#Q10 Swap all of the elements in row 3 of m1 with the elements in column 2 of m1.

m1[3,] #extract row 3 from m1 these numbers need to be changed to the 
m1[,2] #extract column 2 from m1, this is the numbers that need to be added to row 3

m1[3,] <-c (2,9,14,18) #select row 3 and change the numbers in row 3 to those of column 2

#Display m1
m1


#Q11 Create and display a new matrix m3 from m1 and m2 where m3 contains all of the columns of both source matrices

#create m3
m3 <-matrix(c(m1[,1:4],m2[,1:2]), ncol=6) #extract data from matrices m1 & m2

#Display m3
m3


#Q12 Replace all values in m3 that are in (column 1 or column 3) and in (row 2 or row 3) with double their value.

m3[2|3,1|3] <- matrix(c(m3[2|3,1|3]*2)) # replace values in m3 with double the value of column 1 or 3 and row 2 or 3 

#display m3
m3


#Q13 Remove row 2 of m3.

m3<-m3[-2,] #remove row 2

#Display m3
m3


#Q14 Label the columns of m3 as “c1”, “c2”, “c3”, “c4”, “c5”, and “c6”.

colnames(m3)<-c("c1","c2","c3","c4","c5","c6") #name each column as instructed using the colname function

#Display m3 with column names
m3


#Q15 Create a vector mmeans that contains the means of each column

#Apply the mean function to each column

apply(m3,2,mean)

#create a vector containing the means of each column
mmeans<-c(13.3,19.3,24.0,28.0,15.3,19.3)

#Display mmeans
mmeans


#Q16 Create a barplot of mmeans.

barplot(mmeans) #use the barplot function using the data from mmeans.

names(mmeans)<-c("c1","c4","c3","c4","c5","c6") #label the barbplot, column names from matrix m3 have been used in this instance.

#Display barplot using the main function to add a heading and use the density,angle and col functions to change the graphics.
barplot(mmeans,main="Barplott of Vector mmeans",density=20, angle=30, 
        col=c("blue","pink","purple","red","orange","green"))

