# Theresa Darzins Student 1909721

# EXAM PART 1



# Q1 Create a vector with numbers 1 to 16

v1<-(1:16) 
v1 # print out all the numbers in v1



# Q2 Add values 17-20 on the end of v1

v1<-c(v1[1:16],17,18,19,20)
v1 # print out all the numbers in v1



# Q3 Delete elements 5,6,7 and 13 from v1 using one method to remove 5,6,7
#    and another to remove element 13

v1<-c(v1[1:4],v1[8:20])
v1 #print out all the new numbers in v1 (removed 5,6,7 1st method - 
# mentioning all the positives)
v1<-c(v1[-10])
v1 #print out all the new numbers in v1 (removed 13 2nd method -
# mentioning the negative using minus sign)



# Q4 using v1 create new v2 containing vectors
# 1st to 5th and 9th to 11th elements of v1

v2<-c(v1[1:5],v1[9:11])
v2 #print out the new vector v2 



#Q5 calculate v1*v2 and show.  
#   A WARNING will display: Explain how R adds 2 vectors of different lengths

v1*v2
# if we attempt to add (or multiply/divide, etc.) 2 vectors of 
# varied lengths R will recycle the elements of the shorter one 
# to make it the same length as long one so care must be taken to avoid
# doing this



# Q6 create new vector v3 by selecting all even elements of v1 using filter

v3<-c(v1%%2==0)# checks if divisible by 2 and if so prints true
v3 #prints out true or false not elements



# Q7 using loop create v4 the same length as v1 that contains value True if 
#    corresponding element of v1 has integer sq root but value False otherwise

length (v1) # confirming v1 length
v4<-NULL # makes a container id called v4 for info now asking for
for (i in seq(v1))
v4<-c(v4,sqrt(v1[i]))# square root calculation
v4 # displays square roots of each different element of v1
intSqRt<-(v4/round(v4)==1) # shows whole nos divided by each other
# IF answer = 1 its same no. and whole number so True
intSqRt # shows whether argument T/F (True or False)
length (v4)# confirming v4 length



# Q8 Create a matrix m1 with 4 rows from vector v1.

# x<-matrix(c(1,2,3,4,5,6), nrow=2,ncol=4)
# as there are not enough elements (6 v 8) the 1st 2 elements are
# recycled

m1<-matrix(c(v1), nrow=4)
m1



# Q9
# Create a matrix m2 with 2 columns from vector v2 

v2
length(v2)
m2<-matrix(c(v2), ncol=2)
m2



# Q10
# Swap (replace?) all elements in row 3 of m1 with elements in column 2 of m1
# row 3 the one with with 3 10 15 19 is deleted 
# and column 2 of 8 9 10 11 is now in row 3
m1
m1<-rbind(m1[1:2,],c(m1[,2]),m1[4,])#[1:2] leave alone rows 1-2, 
# and row 4 [4, ]
# [,2]before the comma is rows after the comma is columns so 
# replace elements of column 2 with elements of row 3 so 

m1



# Q11
# Create and display a new matrix m3 from m1 and m2 where m3 contains all the 
# ncolumns of both source matrices

m3<-cbind(m1,m2) # column bind cbind not row bind rbind
m3



# Q12
# Replace all values in m3 in (column 3) and in (row 2) with double their value

m3<-cbind(m3[,1:2],c(m3[,3]*2), m3[,4:6])
# leave alone rows 1-2 and 4-6 [,3] refers to column 3 which is x 2
m3<-rbind(m3[1,],c(m3[2,]*2),m3[3:4,]) 
# leave alone row 1 and 3 to 3 and [2, ] refers to row which is x 2
# result 56 as doubled twice (was 14 x 2 x 2)

# 
m3



# Question 13
# Remove row 2 of m3

m3<-m3[-2,]
m3



# Q14
# Label columns of m3 "c1", "c2", "c3", "c4", "c5", and "c6"

colnames(m3)
NULL # makes container for new information
colnames(m3)<-c("c1","c2","c3","c4","c5","c6")# column names changed from 1-6
# to c1-6
m3



# Q15
# Create a vector called mmeans to contain the means of each column

mmeans<-apply(m3, 2, mean) 
# 2, is the mean of columns 
# 1, is mean of rows
mmeans



# Question 16
# Create barplot of mmeans

barplot (
  mmeans, legend.text= "Means c1-c6",
  main = "Bar Chart - Means",
  xlab = "c Data",
  ylab = "Means"
)








































