Vectors in R programming

Vectors in R programming

  • R
  • 4 mins read

In this article, we will study the concepts associated with vectors and how to deal with them in R.

Vectors in R programming are basically an ordering of the elements in such a way that all the elements belong to the same data type. It is a basic data structure in R that is used to store a collection of elements. The elements may belong to any of the data types, be they numerical, logical, string, or complex in nature. 

Since the vectors store the same types of components, they are also known as atomic in nature. Vectors are also known as uni-dimensional arrays. 

A vector may be integer, logical, or character type in nature, depending on the data type that is contained within it. A logical vector, for instance, contains all boolean values.

Declaration of Vector in R

A vector can be declared using three methods in R. We will talk in detail about it. 

Using c() method

A vector in R can be declared using the c() method. All the entries in a vector are comma-separated. An empty vector can be generated by just using the c() method containing no elements. 

#declaring a vector 
vec <- c(5,14,2,46,3, 2)
cat("Vector : ",vec)
The code produces the following output : 
Vector :  5 14 2 46 3 2

Using the colon operator

The colon operator is used to generate consecutive numbers from the starting value specified before the colon to the ending value specified after the colon respectively.

#generating numbers from 1 to 10

vec <- 1:10

cat("Vector : ",vec)
The code produces the following output : 
Vector :  1 2 3 4 5 6 7 8 9 10

Using the seq() Method

The seq() method is in-built in R and also used to produce a sequence, but the interval can be customized. The breaks between the corresponding components of the sequence are specified using the “by” parameter of this method. The syntax associated with this method is : 

seq(st-val , end-val , by )

The arguments of the method are as follows : 

st-val: The starting value of the generated sequence

end-val: The ending value of the generated sequence 

by: The step size 

seq() Method Example

#generating numbers from -1 to -10
#using step size equivalent to -2
vec <- seq(-1,-10,by= -2)
cat("Vector : ",vec)
The code produces the output as : 
Vector :  -1 -3 -5 -7 -9

Length of the Vector in R

A unique attribute of the vector is its length. It represents the number of elements present in it. 

The length of any empty vector is 0. 

#generating numbers from -1 to -10
#using step size equivalent to -2
vec <- seq(-1,-10,by= -2)
cat("Vector : ",vec)
#printing the length of vector
cat("length :", length(vec))
Output
Vector :  -1 -3 -5 -7 -9
length : 5

Vector Indexing in R

Indexing is the terminology associated with the extraction of an element at a specific place from the vector. The indexing takes an index as the parameter and returns the value of the vector stored at that corresponding location. Indexing in R always begins with the value 1. There are three types of indexing in the case of vectors : 

Numeric Indexing

A numerical index position is specified in order to retrieve the vector data value. The index must be within the length of the vector to avoid an error. 

​​#generating numbers from -1 to -10
#using step size equivalent to -2
vec <- seq(-1,-10,by= -2)
cat("Vector : ",vec)
#accessing the first element
cat("First element : ", vec[1])

#accesing the last element
cat("Last element : ", vec[length(vec)])
The code produces the following output : 
First element :  -1
Last element :  -9

Logical Indexing

In the case of logical indexing, a vector of booleans is specified as an index to the vector. The corresponding positions where the indexes are TRUE are returned, eliminating the FALSE positions.

#declaring a vector
vec <- c("foxinfotech","r","oracle","machinelearning","python")
#printing the elements which hold a true value
cat("Vec : ", vec[c(TRUE,FALSE,TRUE,TRUE,FALSE)])

The code produces the following output :

Vec :  foxinfotech oracle machinelearning

Character Indexing

In character indexing, a string or a character key is associated with each element belonging to the vector. Such uni-dimensional arrays are known as associative arrays. The retrieval of elements can then be done by specifying the key to be accessed. 

#generating a vector
vec <- c("Rlang"="Statistics","Python"="DataScience")
print("Element at Rlang pos")
print(vec["Rlang"])
Output
[1] "Element at Rlang pos"
       Rlang 
"Statistics"