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

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

Example 1: Apply numeric() Function to Create Vector of 0s

my_vec1 <- numeric(7)        # Using numeric function in R
my_vec1                      # Showing vector of zeros
# [1] 0 0 0 0 0 0 0

Example 2: Apply integer() Function to Create Vector of 0s

my_vec2 <- integer(7)        # Using integer function in R
my_vec2                      # Showing vector of zeros
# [1] 0 0 0 0 0 0 0

Example 3: Apply rep() Function to Create Vector of 0s

my_vec3 <- rep(0, 7)         # Using rep function in R
my_vec3                      # Showing vector of zeros
# [1] 0 0 0 0 0 0 0

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.
You need to agree with the terms to proceed

Menu
Top