r create vector of zeros – R: Create vector of zeros

Creating vector of zeros in R.

r create vector of zeros: In this article we will see how to create vector of zeros in Python. Mainly we will see three different ways to create vector of zero.

  1. Using integer() function
  2. Using numeric() function
  3. Using rep() function

Sometimes vector of zeros is required in initializing a vector with values and neutralizing it after performing some operations. So let’s see the three different ways how it actually works.

Method-1 : Creating a vector of zeros using integer () function :

zeros in r: In R the integer() function creates objects of vector(integer) of specified length in the argument but each element of vector is 0.

Syntax-integer (length)

Where,

  • length : It represents the length of the vector

Below is the code example of this.

#Program :

# length of vector is 6
zero_vector = integer(6)
cat(" Using integer() function vector of zeros created: " , zero_vector , "\n")
Output :
Using integer() function vector of zeros created:  0 0 0 0 0 0

Method-2 : Creating a vector of zeros using the numeric() function :

Like integer() function in R, the numeric() function creates object of numeric type and it creates double precision vector of the specified length by the argument where all the elements value equal to zero.

Below is the code example of this.

#Program :

# length of vector is 6
zero_vector = numeric(6)
cat("Using numeric() function vector of zeros created: " , zero_vector , "\n")
Output :
Using numeric() function vector of zeros created:  0 0 0 0 0 0

Method-3 : Creating a vector of zeros using the rep() function :

rep() function in R actually replicates the values of the vectors.

Syntax-rep(v, no_of_times)

Where,

  • v : It represents a vector or a factor
  • no_of_time : It represents the number of times each element of the vector to be repeated

Below is the code example of this.

#Program :

zero_vector = rep(0, 6)
cat(" Using rep() function vector of zeros created: " , zero_vector , "\n")
Output :
Using rep() function vector of zeros created:  0 0 0 0 0 0