How to Create a Vector of Zeros in R (5 Examples)

 

In this R tutorial you’ll learn how to declare a vector or array containing only zeros.

The article will contain these topics:

Let’s get started.

 

Example 1: Creating Vector of Zeros Using rep() Function

Example 1 illustrates how to create a vector consisting of zeros using the rep function.

x1 <- rep(0, 5)               # Applying rep() function
x1                            # Printing vector to RStudio console
# 0 0 0 0 0

The RStudio console returns the result of the previous R code: A vector of length five consisting only of zeros.

 

Example 2: Creating Vector of Zeros Using rep() Function & 0L

Alternatively to the R syntax shown in Example 1, we can also specify 0L instead of 0 within the rep function:

x2 <- rep(0L, 5)              # Applying rep() function
x2                            # Printing vector to RStudio console
# 0 0 0 0 0

 

Example 3: Creating Vector of Zeros Using numeric() Function

We can also use the numeric function to construct a vector of zeros. For this, we simply have to specify the length of the vector within the numeric function:

x3 <- numeric(5)              # Applying numeric() function
x3                            # Printing vector to RStudio console
# 0 0 0 0 0

 

Example 4: Creating Vector of Zeros Using integer() Function

Similar to Example 3, we can use the integer function to create a vector of zeros.

x4 <- integer(5)              # Applying integer() function
x4                            # Printing vector to RStudio console
# 0 0 0 0 0

 

Example 5: Creating Vector of Zeros Using c() Function

Last but not least, we can also use the c function to construct an array or vector containing zeros (even though the c function might be inconvenient for longer vector objects):

x5 <- c(0, 0, 0, 0, 0)        # Applying c() function
x5                            # Printing vector to RStudio console
# 0 0 0 0 0

 

Video & Further Resources

Have a look at the following video of my YouTube channel. I’m explaining the R programming syntax of this article in the video:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Besides the video, you may want to have a look at the other articles of my website. Some articles about creating data objects are listed below.

 

To summarize: You learned in this tutorial how to create a vector of zeros in the R programming language. Please let me know in the comments, if you have further questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top