What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Vectors in R | How to Create, Combine & Modify R Vectors

  • Mar 26, 2021
  • 5 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Shivali Bhadaniya
Vectors in R | How to Create, Combine & Modify R Vectors

In this article, we will learn what is a vector in R programming? how to create a vector in R? and how to access it? Also, we will learn the modification of the vector, a combination of two vectors, and the deletion of the vector. So, let’s get started!

What is a Vector in R?

R programming is the most important language used in data science, statistical analysis, and scientific research. Also, the R language is used mostly in machine learning and artificial intelligence. Vectors in the R programming language are similar to arrays in the C programming language. Vectors store the multiple elements of the same data type in a single variable. But unlike the array in C programming, vectors indexing in R language starts from ‘1’ and not with ‘0’. We can store numeric as well as characters in vectors. 

How to Create a Vector in R?

Multiple ways are used to create the vector in the R language.

1) Single Element Vector

Even when a single element is written in the R language, we can consider it a vector. The length of the vector is considered to be 1 and it can be of any data type.

2) Multiple Element vector

  • Using Colon Operator: Using this method we can specify the starting and ending point of the vector. For example:
# Creating a sequence from 2 to 7.
v <- 2:7


 [1] 2 3 4 5 6 7

  • Using Sequence(seq) Operator: Using this method we can define the starting and ending point of the vector along with the incrementing gap. For example:
# Create a vector with elements from 5 to 9 incrementing by 0.5.
print(seq(2, 9, by = 0.5))


 [1] 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0

  • Using c() function: The non-character values are converted into character type if any one of the elements is character. For example:
# logical and numeric values are converted to characters.
s <- c('green', FALSE, 6)


 [1] "green" "FALSE" "6"

What are the types of vectors in R Language?

The vector in R programming can be of different type depending on the data type of the element it contains. Let us study them one by one below:

1) Numeric Vectors

The vectors containing number elements are called numeric vectors. For example:

t <- c(1,2,3)


 [1] 1 2 3

2) Integer Vectors

The vectors containing integer elements are called numeric vectors. For example:

t <- c(6L,7L,8L)


 [1] 6 7 8

3) Logical Vectors

The vectors containing logical values i.e TRUE or FALSE elements are called numeric vectors. For example:




4) Character Vectors

The vectors containing character elements are called numeric vectors. For example:

t <- c("aa","bb","cc")


 [1] "aa" "bb" "cc"

5) Complex Vectors

The vectors containing complex elements are called numeric vectors. For example:

t <- c(12+1i,3i,5+4i)


 [1] 12+1i 0+3i 5+4i

How to find the type of R vector?

To find out the type of the R vector, we can use typeof() function. For example:

t <- c(1,2,3)
v <- c(6L,7L,8L)
u <- c("aa","bb","cc")


 [1] "double"

 [1] "integer"

 [1] "character"

How to Access a Vector in R?

The Vectors in the R language are the same as arrays in the C language. Therefore, we can access the elements of vectors using the indexes just like arrays. The square brackets[] are used for indexing. To drop particular elements while accessing vectors negative indexing. Also, while accessing the vectors in R we can use TRUE and FALSE or 0 and 1. For example:

# Accessing vector elements using an index.
t <- c("Jan","Feb","Mar","Apr","May","Jun")
u <- t[c(1,3,5)]
# Accessing vector elements using logical indexing.
# Accessing vector elements using 0/1 indexing.
y <- t[c(0,0,0,0,1,0)]

[1] "Jan" "Mar" "May"
[1] "Jan" "Mar" "May"
[1] "Sun"


 [1] "Jan" "Mar" "May"

 [1] "Jan" "Mar" "May"

 [1] "Sun"

How to Modify a Vector in R?

In the R programming language, we can modify the vector using the assignment operator. Modification of vector can be done in many ways as shown in the below:

# Creating a vector 
X <- c(2,3,4,5,6,7,8,9) 

# modify a specific element 
X[2] <- 1
X[3] <- 9
cat(X, '\n') 


 2 1 9 5 6 7 8 9

How to Combine Vectors in R?

In R programming, we can combine two vectors using c() function. For example:

t = c(1, 2, 3) 
 v = c("a", "b", "c") 
 c(t, v) 


 [1] "1" "2" "3" "a" "b" "c"

How to Delete a Vector in R?

Deletion of the vector is the process of deleting all the elements of the vector. This can be done by assigning it to the NULL value. For example:

# Creating a Vector 
t <- c(2, 3, 4, 5) 

# set NULL to the vector 
t <- NULL 
cat('Output vector', t)


 Output vector


Vectors are the most basic data structure of R programming. It is the collection of the same data type under one variable. Therefore, in this article, we understood the creation, and deletion of vectors along with different operations used to access the vector, modify the vector, and combine any two given vectors. Now, you can easily create a vector in R.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Shivali Bhadaniya
I'm Shivali Bhadaniya, a computer engineer student and technical content writer, very enthusiastic to learn and explore new technologies and looking towards great opportunities. It is amazing for me to share my knowledge through my content to help curious minds.