Vectors in R Programming – Everything You Need to Know

A vector is a basic data structure in R that represents a one-dimensional array of items that are of the same type. In this tutorial, we will deep dive to understand what are vectors in R.

Table of Contents

What are Vectors in R?

Vectors in R is a sequence of elements which share the same data type. There are six primary types of vectors in R

    • Integer: Containing integers values.
    • Logical: Containing TRUE or FALSE values.
    • Double: Contains sequence of Real Numbers with decimals.
    • Complex: Contains Complex Numbers.
    • Character: Contains strings or text.
    • Raw: Contains raw bytes.

Vectors are important as they allows the users to efficiently manage data storage and manipulation of the same.

Creating Vectors in R

There are several different ways in which we can create a vector in R Programming. Let’s have a look on some of them:

1. Using c() Function

The c() function or (concatenate or combine) is the most common way to create a vector by combining multiple elements.

# Numeric vector
numeric_vector <- c(1, 2, 3, 4, 5)
print(numeric_vector)
# Output: [1] 1 2 3 4 5

# Character vector
char_vector <- c("apple", "banana", "cherry")
print(char_vector)

# Output: [1] "apple" "banana" "cherry"

2. Using the Colon Operator

The colon operator (:) generates a number sequence with a one-digit difference between subsequent integers.

# Sequence from 1 to 5
seq_vector <- 1:5
print(seq_vector)


# Output: [1] 1 2 3 4 5

3. Using the seq() Function

The seq() function allows you to specify the increment value, giving you more flexibility when creating sequences.

# Sequence from 1 to 5 with increment of 0.5
seq_vector <- seq(1, 5, by = 0.5)
print(seq_vector)

# Output: [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0

Types of Vectors in R

Vectors are classified in two ways: according to the number of elements and according to the data types. We already discussed the many forms of vectors based on datatypes. Now let’s look at vectors based on the number of elements.

1. Based on Number of Elements

Vectors in R can be categorized according to how many elements they include. Here are the two types of vector based on number of elements present inside them.

    • Single Element Vector
    • Multiple Elements Vector

1.1. Single Element Vector

 Single-element vector in R is a vector that contains only one element.

#Atomic vector of integer type
print(52L)
#Logical type
print(TRUE)

Output:

sol <- nchar("Counting number of
[1] 52      
[1] TRUE

1.2.Multiple Elements Vectors

A Multiple-elements vector in R is a vector that contains more than one element

my_vector <- c(1, 2, 3, 4, 5) 
my_vector <- c("apple", "banana", "orange") 
my_vector <- c(TRUE, FALSE, TRUE)

Accessing Vector Elements

Indexing allows you to access elements within a vector. R follows 1-based indexing, which means that the first element is at position one. There are three common indexing practice, through which one can access elements in the vectors

1. Positive Indexing

Specify the items’ positions so that you can access them.

# Adding two vectors
vector1 <- c(1, 2, 3)
vector2 <- c(4, 5, 6)
sum_vector <- vector1 + vector2
print(sum_vector)

# Output: [1] 5 7 9

2. Negative Indexing

Use negative indices to exclude specific elements.

# Excluding elements at positions 1 and 3
vector <- c(10, 20, 30, 40, 50)
subset_vector <- vector[c(-1, -3)]
print(subset_vector)

# Output: [1] 20 40 50

3. Logical Indexing

Use logical vectors to select elements that meet certain conditions.

# Selecting elements greater than 25
vector <- c(10, 20, 30, 40, 50)
subset_vector <- vector[vector > 25]
print(subset_vector)


# Output: [1] 30 40 50

Vector Operations

1. Arithmetic Operations

Two vectors having the same length can do arithmetic operations such as addition, subtraction, multiplication, and division to get vector output.

# Adding two vectors
vector1 <- c(1, 2, 3)
vector2 <- c(4, 5, 6)
sum_vector <- vector1 + vector2
print(sum_vector)


# Output: [1] 5 7 9

2. Vector Element Recycling

If two vectors of different lengths are involved in an operation, R recycles the shorter vector to match the length of the longer one.

# Recycling shorter vector
vector1 <- c(1, 2, 3, 4)
vector2 <- c(10, 20)
sum_vector <- vector1 + vector2
print(sum_vector)

# Output: [1] 11 22 13 24

3. Sorting Vectors

The sorting of elements in a vector takes place in ascending or descending order. It can be either numbers or characters.

Example:

# Sorting in ascending order
vector <- c(3, 1, 4, 2, 5)
sorted_vector <- sort(vector)
print(sorted_vector)


# Output: [1] 1 2 3 4 5

Output:

[1] -6 0 2 5
[1] "Blue" "Green" "Red"

Conclusion

Vectors are the fundamental building blocks of data manipulation in R, providing strong and easy methods for storing and processing data quickly. Understanding vector construction, indexing, and operations allows you to simplify data analysis and create more efficient R scripts.

If you want to improve your data science skills and obtain hands-on experience with R programming, consider taking a data science course.

Our Data Science Courses Duration and Fees

Program Name
Start Date
Fees
Cohort Starts on: 4th May 2025
₹69,027
Cohort Starts on: 27th Apr 2025
₹69,027
Cohort Starts on: 13th Apr 2025
₹69,027

About the Author

Principal Data Scientist

Meet Akash, a Principal Data Scientist with expertise in advanced analytics, machine learning, and AI-driven solutions. With a master’s degree from IIT Kanpur, Aakash combines technical knowledge with industry insights to deliver impactful, scalable models for complex business challenges.