R: Create empty vectors and add new items to it

Empty vectors in R are the vectors of length zero. We often need to create empty vectors in a
program so that elements can be added later as and when required. While there are several ways to do the same, we will be talking about few of them.

In this article will learn how to create empty vectors in R and then add elements to it in many ways.

1. using c() function
2. assigning NULL
3. using rep() function
4. using vector() function
5. using numeric() function

Let us start with the first approach creating an empty vector using c() function but before that let us also know little bit about length() function and is.null() function as we will be using both the functions to verify the length of the vector and finding out if the vector is NULL or not.

length(x) function will return the length of the vector/ data object passed in the argument (x in this case)

is.null(x) function will return TRUE in case the vector/data object passed in the argument is NULL else will return FALSE .

Creating empty vector using c() function

The c() function provides one of the primary ways to create vectors in R ,it combines its arguments to get a vector of a common type.

The below example code will create a vector my_vector of length zero and verifying the length and type of the vector.

my_vector <- c()
cat("create empty vector using c function : ",my_vector,"\n")
cat("length of my_vector : ",length(my_vector),"\n")
is.null(my_vector)

In the below output we can see that nothing is printed on console if we try printing an empty vector, length of the vector is zero and the vector is NULL.

Output :-

create empty vector using c function : 
length of my_vector :  0 
TRUE

Now, let us look at how to add new items to my_vector.

my_vector <- c(1:7)
cat("adding elements to my_vector : ",my_vector,"\n")
cat("length of my_vector : ",length(my_vector),"\n")
is.null(my_vector)

Output :- 

adding elements to my_vector : 1 2 3 4 5 6 7 
length of my_vector :  7 
FALSE

Observe the length of vector in the output.

 Creating empty vectors using NULL

Assigning NULL to any vector will create an empty vector of  NULL type. NULL is null object in R used to represent vectors/data objects with zero length.

Let us go through the below example code.

my_vector <- NULL
cat("create empty vector using NULL : ",my_vector,"\n") 
cat("length of my_vector : ",length(my_vector),"\n")
is.null(my_vector)

In below output empty vector my_vector has been created with length zero and is NULL.

Output :- 

create empty vector using NULL :  
length of my_vector :  0 
TRUE

Adding elements to my_vector.

my_vector <- c(1:8)
cat("adding elements to my_vector : ",my_vector,"\n")
cat("length of my_vector : ",length(my_vector),"\n")
is.null(my_vector)

Output :- 

adding elements to my_vector :  1 2 3 4 5 6 7 8 
length of my_vector :  8 
FALSE

Creating empty vector using rep() function

The rep() function will replicate the values in the vector number of times passed in the argument.
Syntax:   rep(x,times)
Arguments : ‘x’ – vector or the element(text, numeric…) which has to be repeated and ‘times’ – repetition of values.

In the below code we will be creating a vector of  zero length. We will do the same using ‘NULL’ as well as ‘NA’, both will be creating empty vectors but of different types.

  • creating  empty vector with rep() function and ‘NA’

‘NA’ in R indicates a missing value also termed as Not Available. 

In the below code we are creating an empty vector of length zero and then printing the length on the console. We are also verifying if the vector is null and printing the type of vector created. In this case the type of the vector will be logical.

vec_rep <- rep(NA,0)
cat("create empty vector using rep function()  : ",vec_rep,"\n")
cat("length of vec_rep : ",length(vec_rep),"\n")
is.null(vec_rep)
cat("type of vec_rep: ",typeof(vec_rep),"\n")

Output :- 

create empty vector using rep function()  :   
length of vec_rep :  0 
FALSE
type of vec_rep:  logical 
  • creating  empty vector with rep() function and ‘NULL’

Writing the similar code.

vec_rep <- rep(NULL,0)
cat("create empty vector using rep function()  : ",vec_rep,"\n")
cat("length of vec_rep : ",length(vec_rep),"\n")
is.null(vec_rep)
cat("type of vec_rep : ",typeof(vec_rep),"\n")

Output :- 

This time is.null(vec_rep) returned TRUE.

create empty vector using rep function()  :  
length of vec_rep :  0 
TRUE
type of vec_rep :  NULL 

Adding elements to vec_rep

In the below code example we will be adding boolean values to the vector and verifying the length and type in the output.

vec_rep <- c(TRUE,FALSE,TRUE,TRUE)
cat("adding elements to vec_rep : ",vec_rep,"\n")
cat("length of vec_rep : ",length(vec_rep),"\n")
is.null(vec_rep)
cat("type of vec_rep : ",typeof(vec_rep),"\n")

Output :- 

adding elements to vec_rep :  TRUE FALSE TRUE TRUE 
length of vec_rep :  4 
FALSE
type of vec_rep :  logical 

Observe the type of the vector is logical now.

Creating empty vector using vector() function

Let us see how to create empty vectors using vector() function , vector() function produces the vector for a given length and mode in arguments.
Syntax:  vector(mode=”logical”,length=0) ;  default mode is logical and default length is zero.
Therefore, to create a vector of zero length we will write the below code :

vector_new <- vector()
cat("create empty vector using vector() function  : ", vector_new,"\n")
cat("length of vector_new : ",length(vector_new),"\n")

Output :- 

create empty vector using vector() function  :   
length of vector_new :  0 

Adding new items to vector_new

vector_new <- vector(mode="numeric",length=10)
cat("adding elements to vector_new : ",vector_new,"\n")
cat("length of vector_new : ",length(vector_new),"\n")

Output :- 

adding elements to vector_new :  0 0 0 0 0 0 0 0 0 0 
length of vector_new :  10 

Creating empty vector using numeric () function

This numeric() function in R creates a double precision vector of the length specified in the argument with all values zero.
Syntax: numeric(length)
Argument: length specifies the length of which the vector will be created and default value of argument length is zero.
Therefore, to create a vector of zero length we will be writing the below code.

vec_num <- numeric(0)
cat("create empty vector using numeric() function  : ",vec_num,"\n")
cat("length of vec_num : ",length(vec_num),"\n")

Output :-

create empty vector using numeric() function  :   
length of vec_num :  0 

Adding new items to vec_num

vec_num <- c(vec_num, 1:5)
cat("add elements to vec_num : ",vec_num,"\n")
cat("length of vec_num : ",length(vec_num),"\n")

Output :- 

add elements to vec_num  :  1 2 3 4 5 
length of vec_num :  5 

 

** Know more about HOW TO CREATE VECTORS in R

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