Vector In R - A Practical Approach For Creating And Using Vector In R Language


R is an important programming language which is used by statisticians. It is also used in machine learning, data science, research, and many more new fields. In this article, we shall learn one of the important concepts of R by writing and executing that code in R Studio Editor.

Let's start now with the important concepts of R, i.e., working with Vector in R.

Vector in R

Vector is one of the basic data structures in R. It basically contains the same data type of elements. It can contain any type of data like logical, integer, character or double. We can say it is just like the array we use in programming languages C, C#, Java, or others.

Methods of Creating Vector

There are different ways of creating a vector in R which are below.
  • Using rep() function
  • Using colon (:) operator
  • Using c() function
  • Using seq() function
Make sure all of the above methods and functions are case sensitive. Now, we shall discuss each method and how to use it in R script.
  • Using rep() function 
    This is the simplest way of creating a vector in R. Below is the code to create a vector in R.
    1. rep("Suraj", 3)  
    2. Output  
    3. [1] "Suraj" "Suraj" "Suraj"   
  • Using colon (:) operator 
    Below is the code to create a vector in R.
    1. myVector <- 1:5  
    2. myVector  
Output: 1 2 3 4 5

Here, myVector is the name of vector created by colon(:) operator. We can visualize it in the below image. It has printed numbers from 1 to 5. The colon (:) method of creating a vector is very useful when we want to generate consecutive numbers.

  • Using c() function
    We can create a vector using c() function as below. It is very simple as seen below.
    1. mySecondVector <- c(1, 5, 4, 9, 0)  
    2. mySecondVector  
Output: 1 5 4 9 0

In R Studio, the above code for creating vector works, as shown in the image below.

  • Using seq() function
    We can create a vector using seq() function as below.
    1. myThirdVector <- seq(1, 15, by=3)  
    2. myThirdVector  
    Output: 1 4 7 10 13.

    Here, 1 is the starting point and 15 is the last point to be generated by the vector. The third parameter is used for defining the step of the vector. The value of step can be positive or negative, hence we can pass any positive or negative numbers . We can also pass decimal values.
In the below image, it shows how seq() works in R script.


Difference between using a colon(:) and seq() function for creating R Vector

Although the colon(:) operator and seq() function behave in the same way while creating a vector, one of the major advantages of using the seq() function is that we can provide step value in the operator. It has been shown in the above image. Below is one more example which shows how to use decimal value in step.


Fixing the length of Vector - Use of length.out Operator

We can fix or limit the number of elements in a vector by using length.out operator. It has been shown in the below image. We can see clearly that we have used same function seq() and same values the in parameter. It has created a vector of 20 elements without using length.out operator while the in the second case, it has created a vector of lenth 7 because we have passed length.out parameter value 7 to limit the size of the vector.


Checking Type of Vector

Checking datatype of the vector in R is same as when we check the datatype of other elements using typeof() function. The below example illustrates how it works in R.
  1. myIntegerVector <- 1:5  
  2. myIntegerVector  
  3. typeof(myIntegerVector)  

We apply a similar approach to know the datatype of the vector as the one we apply with other types of element to know their datatype. For example, we below we have created and checked datatype of below vector as -
  1. myCharacterVector <- c("Suraj""Mahesh""Suresh")  
  2. myCharacterVector  
  3. typeof(myCharacterVector)  
The output will be a character, as shown in the below image.


Accessing Elements of Vector in R

An element of the vector is accessed by their position. The position is also called index. The index of a vector start with 1 unlike in most other programming language it start with 0.

Below example show how to access the element of vector by index.
  1. > myPreferredLanguage <- c("C""C#""C++""Java")  
  2. > myPreferredLanguage[1]  
  3. [1] "C"  
  4. > myPreferredLanguage[2]  
  5. [1] "C#"  
  6. > myPreferredLanguage[3]  
  7. [1] "C++"  
Here, myPreferredLanguage is the vector of 4 elements of character type. Now, to get the first element, we have used myPreferredLanguage[1] and for getting the second element we have used myPreferredLanguage[2] and so on.

More precisely, it has been shown in the below image which has been written in R script in R studio.


If you will try to access the vector element by providing index value 0 then it will give the vector type with 0 index. It's the same if you try to access the elements of the vector by providing the index value outside the maxium index value then it will print NA. You can see it in the below image.


Accessing Elements of a Vector in R by Element Name

We can access the elements of a vector by the name of an individual element of the vector.

Let's how to give the name of the element of the vector in R and accessing them by name.

Naming of Vector element in R

We use names() function for giving the name of vector element. It has been shown below. First, we have created a vector of without giving element name. Then, the name of a vector is myVectorWithoutName.
  1. > myVectorWithoutName <- c("C""C#""C++""Java")  
  2. > myVectorWithoutName #It is the output of vector without name  
  3. [1] "C" "C#" "C++" "Java"  
Now, we will give the name of individual elements of above vector as below,
names(myVectorWithoutName) <- c("a", "b", "c", "d") #Naming of element
  1. myVectorWithoutName  
As you can see we have used names() function for giving the name of vector element. Now, when you will print this vector, it will give the vector output as below.
  1. > myVectorWithoutName  
  2. a b c d  
  3. "C" "C#" "C++" "Java"  
Here a, b, c, and d are the names of the vector. In this way, we have seen how we can give the name of vector element in R.

The below image shows how it looks and works in R script when executed in R Studio.


So far, we have seen how to give the name of an element of a vector in R. Now, we will see how to access the elements of the vector by their name. 

Accessing Vector Element by Element Name

Accessing vector elements by their name is quite easy. We follow the same approach for accessing vector elements as we have done in accessing vector elements by index. We use [] brackets in both cases. The only difference is that we pass the name of the element while accessing by name and index value while accessing by element name.
  1. names(myVectorWithoutName) <- c("a""b""c""d") #Naming of element  
  2. myVectorWithoutName[2] #2nd element by index  
  3. myVectorWithoutName["b"] #2nd element by element name  
Output in both case,

where b is the name of the element.

It has been shown in the below image which has been executed in R studio in R script.


Changing or Modifying the Vector Element 

To change the vector element value in R, simply we can use assignment operator with their index or name, as shown below.
  1. myVectorWithoutName <- c("C""C#""C++""Java")  
  2. myVectorWithoutName  
  3. myVectorWithoutName[2] <- "F#" #Changing element by index  
  4. myVectorWithoutName  
  5. names(myVectorWithoutName) <- c("a""b""c""d") #Naming of element  
  6. names(myVectorWithoutName) #Printing the vector name  
  7. myVectorWithoutName["c"] <- "J#" #Changing element by name  
  8. myVectorWithoutName  

Reversing the Order of Vector Element in R

The order of an element can be changed by using rev() function. It is used as below.
  1. rev(1:10)   
output: [1] 10 9 8 7 6 5 4 3 2 1 
Using Vetor in Looping

We can use vector in R in different ways. Here we shall discuss how we can use vector for print like looping. Below is one basic example of vector used for printing a statement multiple times using vector and a for loop.
  1. for( i in 1:5) {  
  2.    print("We are learning vector in ")  
  3. }  

[1] "We are learning vector in R"
[1] "We are learning vector in R"
[1] "We are learning vector in R"
[1] "We are learning vector in R"

The below image shows the concept of how to use vector in loop.



In this article, we have learned several concepts of using vector in R language step by step, practically. We have learned the different ways of creating and accessing a vector in R. We have also shown how to use a vector in a simple for loop.

I hope you have learned and enjoyed reading this article. You’re welcome to like, comment, share and leave any type of suggestion for my appreciation.


Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now