To understand the vector concept and creation of single and multiple element vector.
Vectors are the most basic R data objects and there are six types of atomic vectors. They are logical, integer, double, complex, character and raw.
Creating a single element vector :
Writing single value in R is taken as a vector of length 1 and belongs to any one class of the vector(i.e Numeric,character,complex,Logical) .
Creating a multiple element vector :
Multiples element vector can be created in different ways.
Using the colon (:) operator
Using the colon operator :
Using seq() :
By=value is the step size
Using c() :
Accessing vector :
Elements of a vector can be accessed by using .
The index value starts from 1
Giving negative index drops the particular element
Vector arithmetic :
Two vectors can be added,subtracted,multiplied and divided whose output is also a vector
Vector element Recycling :
If we apply arithmetic operations to two vectors of unequal length, then the elements of the shorter vector are recycled to complete the operations.
The length of longer vector should be multiples of the length of shorter vector.
Vector sorting :
The elements of the vector can be sorted using the sort() function.
To sort the elements in descending order set the argument decreasing=TRUE
Source code : (Vector creation and accessing)
#single element vector
cat(“A = “,a,”is of class “,class(a))
cat(“\nB = “,b,”is of class “,class(b))
cat(“\nC = “,c,”is of class “,class(c))
#muliple element vector
cat(“\nValue of the vector a is “,a,”and it is of type “,class(a))
cat(“\nValue of the vector b is “,b,”and it is of type “,class(b))
cat(“\nValue of the vector d is “,d,”and it is of type “,class(d))
#accessing vector using indexing
cat(“\nThe third element of the vector d is “,d,”\n”)
#using negative index drops the element
Source code : (Vector arithmetic,element recycling,sorting)
#vector element recycling