WHAT IS A VECTOR
VECTOR CREATION METHODS
v=c(1,2,3,4,5) ## Method 1. Use function c.This will give us a vector starting from 1 to 5.
v<-seq(1,10,by=2) ##Method 2. Use seq function
## seq will generate the sequence as per start value, end value and difference between each term
v<-rep(2,10) ## Method 3. Use repeat (rep) function
## repeat function will repeat the value n number of times.
v<-1:20 ## Method 4. Use : between start value and end value
FUNCTIONS ON VECTORS
## Function to find length of vectorlength(v)
## Function to set the name of each element of vector
v<-c(1,2,3,4,5,6,7)
names(v)<-c("Sun","Mon","Tue","Wed","Thr","Fri","Sat")
## Function to calculate the sum of vectors
sum(v)
## Function to calculate the product of vectors
prod(v)
## Function to sort the vector
sort(v)
## Function to sort the matrix indexes according to the values.
order(v)
ACCESSING AND MODIFYING VECTORS
## How to access elements of vectorv[1] ## first element of vector
v[length(v)] ## last element of vector
v[length(v-1)] ## last but one element of vector
v[v>3] ## list of all element where are > 3
v[v<5] ## list of all element which are <5
v[4]<-7 ## It will modify the fourth element of vector
v[1:3] ## It will return first three elements of vector
v[c(1,3,5)] ## It will return first,third and fifth elements of vector
VECTOR OPERATIONS
## We can perform mathematical operations on two vectors as we do on numbers.v1<-c(1,2,3,4)
v2<-c(5,6,7,8)
v1+v2 ## Addition of two vectors
[1] 6 8 10 12
v2-v1 ## Subtraction of two vectors
[1] 4 4 4 4
v2*v1 ## Multiplication of two vectors
[1] 5 12 21 32
v2/v1 ## Division of two vectors
[1] 5.0 3.0 2.33 2.00
## We can perform mathematical operations on vectors and numbers.
v1+10 ## Addition of vector and number
[1] 11 12 13 14
v1*2 ## Multiplication of vector and number
[1] 2 4 6 8
## We can perform operations between different sized vectors
v1<-c(1,2,3,4)
v2<-c(1,2)
v1+v2 ## Addition of a small and big vector
[1] 2 4 4 6
## The small vector is recycled so that its size can be matched with large vector, This is called recycling of vectors
NESTED METHOD OF VECTOR CREATION
## vectors can be created by combining two or more methodsv<-c(rep(1,3),rep(2,3),rep(3,3),rep(4,3)) ## rep inside a c function
[1] 1 1 1 2 2 2 3 3 3 4 4 4
v<-rep(c(1,2,3),4) ## c inside a rep function
[1] 1 2 3 1 2 3 1 2 3 1 2 3
MERGING TWO VECTORS
v1<-c(1,2,3,4,5)
v2<-c(6,7,8,9,10)
## These two vectors can be merged by passing into one vector.
v<-c(v1,v2) ## create a new vector after merging v1 and v2
v2=v1 ## update v2 with the values of v1
Questions
## Q1)What is recycling of elements in a vector? Give an example
A##When two vectors of different length are involved in a operation then the elements of the shorter vector are reused to complete the operation. This is called element recycling. Example - v1 <- c(4,1,0,6) and V2 <- c(2,4) then v1*v2 gives (8,4,0,24). The elements 2 and 4 are repeated.
## Q2)How will you check if an element 2 is present in a vector?
It can be done using the grep() or match () function.
1) grep() function returns the location of all matching values.
v<-c(2,4,6,8,2)
grep(2,v)
[1] 1 5
2) match() function returns the first location of element 2
match(2,v)
[1] 1
3) is.element(2,v)
[1] TRUE
## Q3) What will be the class of the resulting vector if you concatenate a number and a character?
character
## Q4) What will be the result of multiplying two vectors in R having different lengths?
The multiplication of the two vectors will be performed and the output will be displayed with a warning message like – “Longer object length is not a multiple of shorter object length.” Suppose there is a vector a<-c (1, 2, 3) and vector b <- (2, 3) then the multiplication of the vectors a*b will give the resultant as 2 6 6 with the warning message. The multiplication is performed in a sequential manner but since the length is not same, the first element of the smaller vector b will be multiplied with the last element of the larger vector a.
##Q5) what will be the result of multiplying a vector with matrix?
##Q6) What is the output of rep(1,3):rep(3,3)
##Q7) How to find the index of maximum element of vector