4 Ways to Create a Vector in R

Creating a vector is easy, and there are four ways to generate it. Each method creates a vector, but you can use the specific method for specific scenarios. Elements of a vector are called Components.

What is Vector in R?

R Vector is a fundamental data structure that contains elements of the same type. Use the length() function to find a vector’s length. The length means how many elements are there in the vector.

A vector is a sequence of elements that share the same data type. Vector can hold single or multiple values of a single type.

Vector can contain various data types like integer, double, character, logical, complex, or raw.

Create Vector in R

To create a vector in R, use the c() function. The c() is a built-in function that combines its arguments.

rv <- c(11, 46)



[1] 11 46

You can see that we created a vector rv using the c() function. It has two elements, 11 and 46. 

To get the length of a vector, use the length() function.

rv <- c(11, 46)



[1] 2

Creating a vector using the : operator

The colon(:) operator helps us create a vector of consecutive numbers.

rv2 <- 1:11




[1] 1 2 3 4 5 6 7 8 9 10 11
[1] 11

Using the colon operator in this example, we created a vector of consecutive numbers.

Create a regular sequence vector using the seq() function

The seq() is a built-in R function that generates the general or regular sequences from the given inputs.

rv3 <- seq(1, 25, by = 5)


[1] 1 6 11 16 21
[1] 5

To create a regular sequence vector, use the seq() function and declare the step size using the by parameter.

Using assign() function

The assign() is a built-in R function that assigns a value to a name in an environment.

print(assign("rv4", c(19, 21, 11, 46)))


[1] 19 21 11 46

You can see that we created a vector that has four elements using the assign() function.


  1. The best way to create a vector in R is to use the c() function.
  2. To create a vector of consecutive numbers, use the : operator.
  3. To create a vector of regular sequence, use the seq() function.

That’s it.

Related posts

How to create an empty vector

as.vector() in R

Leave a Comment