Creating Vectors in R Programming (6 ways)

Vectors are fundamental data structures in R with logical, integer, double, character, and raw data types. Vectors are somewhat similar to what is arrays in C.

In this article, we will be learning how to create vectors in R using some primary ways:

  1. using c() function.
  2. using ‘:’
  3. using rep() function
  4. using vector() function
  5. using seq() function
  6. using a combination of different functions

Creating vector in R using c() function

In R programming language, we can create vectors using c() function. This c() function combines its arguments to form a vector of a common type.

Now we will be creating our vector named num_vec using the c() function with elements 1,2,3,4,5.

num_vec <- c(1,2,3,4,5)
cat("creating vector using c() function is : " , num_vec , "\n")

Output: –

creating vector using c() function is :  1 2 3 4 5 

The type of the vector num_vec will be double. Let’s verify its type

typeof(num_vec)

Output:-

"double"

Creating vector in R using ‘:’

Another way to create vectors is through ‘:’. Let’s see how to do that. The below code will create a vector named num_vec in R of type integer with elements 1,2,3,4,5.

num_vec  <- 1:5
cat("vector created using colon is : " , num_vec , "\n")

Output :- 

vector created using colon is :  1 2 3 4 5 

Verifying its type

typeof(num_vec)

Output:- 

"integer"

Creating vector in R using the rep() function

We will now be creating a vector using the rep(element, times) function. This function will replicate the values in the vector as specified in its arguments. It takes the arguments as vector or elements(text, numeric ..) and the number of repetitions.

We will see with an example, the below code will create a vector with element 4 repeated 5 times.

rep_vec <- rep(4,5)
cat("create vector using rep function : ",rep_vec,"\n")

Output:- 

create vector using rep function :  4 4 4 4 4 

Creating vector in R using the vector() function

vector() function can also be used to create the vector. vector(mode, length) function will create vector based on the arguments passed, type and length. The below code creates a vector of logical type of length 5.

vec_logical <- vector("logical" ,5)
cat("create vector using vector() function  : ",vec_logical,"\n")

Output:- 

create vector using vector() function  :  FALSE FALSE FALSE FALSE FALSE 

We will now verify its type

cat("type of vec_logical: ",typeof(vec_logical),"\n")

Output:- 

type of vec_logical:  logical 

Creating vector in R using seq() function

We can also create vectors in R using the seq(from,to,by) function. The vector created by this function will have elements in a specific sequence. Let us know a bit more about seq() function before proceeding.

syntax:

seq(from, to, by) where from is the starting of the sequence ,  to is the end value(maximum possible) of the sequence and by is the increment of the sequence.

default argument values

from = 1 , to = 1, and by = (to – from)/(length.out – 1))

We will now be creating the vector using seq() function

vector_seq <- seq(from = 1, to = 10 , by = 2)
cat("create vector with sequence values : ",vector_seq,"\n")

Output:-

create vector with sequence values :  1 3 5 7 9

Another way to use the seq function is shown below and will yield the same results and vector of type double will be created.

vector_seq <- seq(1, 10 , 2)
cat("create vector with sequence values : ",vector_seq,"\n")

Output:-

create vector with sequence values :  1 3 5 7 9 

We can now verify the type of the vector created by above code.

cat("type of vectorwithsequence: ",typeof(vector_seq),"\n")

Output:- 

double

Creating vector in R using a combination of different functions.

There are times when we need to combine various functions to create vectors based on our requirements. We will be looking into few examples which illustrate how to create vectors combining different functions.

a. Create Vectors using combination of seq() and c() function

The below code will create a vector having a sequence between 1 and 6 taking an increment of 2 and finally appending it with elements 20 and 9.

vector1 <- c(seq(1,6,2),20,9)
cat("create vector using seq() and c() function : ",vector1,"\n")

Output :- 

create vector using seq() and c() function :  1 3 5 20 9 

b. Create Vectors using combination of seq() and rep() function

The below code will create a vector having a sequence starting from=1, to= 6 taking an increment of 2 and repeating 3 times

vector2 <- rep(seq(1,6,2),3)
cat("create vector using seq and rep : ",vector2,"\n")

Output :-

create vector using seq and rep :  1 3 5 1 3 5 1 3 5 

c. Create Vectors using combination of ‘:’ and c() function

The below code will create a vector with elements 1,2,3 and then appending elements 4 and 5 to it.

vector3 <- c(1:3,4,5)
cat("create vector using ':' and c() function : ",vector3,"\n")

Output :- 

create vector using ':' and c() function : 1 2 3 4 5.

Summary

There are number of ways for creating the vectors in R, few of them have been mentioned above. We can use any one of them or in combination depending on the requirements.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top