Empty vectors in R are the vectors whose length is zero. There are several ways to create empty vectors and then add elements to them. Vector is a basic data structure that contains elements of the same type. Let’s see how to create or initialize the empty vector in R.
How to Create an Empty Vector in R
There are the following ways to create an empty vector.
- Using vector() method
- Using c() method
- Using numeric() method
- Using rep() method
- Assigning NULL to an existing vector.
Create an empty Vector using the vector() method
To create an empty vector in R, use the basic vector() method, and don’t pass any parameter. By default, it will create an empty vector.
rv <- vector()
rv
Output
logical(0)
To check the length of the vector, use the length() method.
rv <- vector()
length(rv)
Output
[1] 0
The length() method will return the length of the vector object passed in the argument.
Now, you can add elements to the vector.
rv <- vector()
length(rv)
cat("After adding elements to an empty vector", "\n")
rv <- c(1:7)
rv
length(rv)
Output
[1] 0
After adding elements to an empty vector
[1] 1 2 3 4 5 6 7
[1] 7
And you can see from the output that we have added the elements to the vector.
Using the c() method to create an empty Vector
The c() method is mainly used for creating a vector in R. We can also use the c() method to empty the vector.
rv <- c()
rv
cat("length of rv : ", length(rv), "\n")
is.null(rv)
Output
NULL
length of rv : 0
[1] TRUE
As you can see, we created an empty vector using the c() function.
Using numeric() function
The numeric() function in R creates a double-precision vector of the length specified in the argument with all values zero.
So, we will create a vector with 0 length using the numeric() function.
rv <- numeric(0)
rv
cat("length of rv : ", length(rv), "\n")
Output
numeric(0)
length of rv : 0
Using rep( ) function
The rep() function replicates the values in the input data. It is a generic function.
rv <- rep(NULL, 0)
rv
cat("length of rv : ", length(rv), "\n")
Output
NULL
length of rv : 0
Here, you can use the is.null() function to check if the vector is NULL or not. The is.null() function returns a boolean vector that is either TRUE or FALSE.
You can also create an empty vector with the rep() function and NA. The “NA” in R designates a missing value, also known as Not Available.
rv <- rep(NA, 0)
rv
length(rv)
is.null(rv)
Output
logical(0)
[1] 0
[1] FALSE
As you can see that the rep() function with NA returns an empty vector, but it has not NULL value.
Assigning NULL to an existing vector
If you assign any vector to a NULL value, then that existing vector will become empty of NULL type.
rv <- 1:5
rv
length(rv)
rv <- NULL
rv
length(rv)
is.null(rv)
Output
[1] 1 2 3 4 5
[1] 5
NULL
[1] 0
[1] TRUE
First, we initialized a vector with five elements and then assign the NULL value to empty the vector of type NULL.
Conclusion
We have seen many ways to create an empty vector, and based on your requirements, and you can use one of the above approaches. You can use the c() function, vector() method, rep() or assign a NULL value to the existing vector or initialize the vector with NULL.
See also
How to Create Empty DataFrame in R

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.
A buddy of mine recommended your blog on Facebook. I can see why. Great post here.