How to create a vector with zero values in R Program?

R ProgrammingServer Side ProgrammingProgramming

In data analysis, sometimes we need to use zeros for certain calculations, either to nullify the effect of a variable or for some other purpose based on the objective of the analysis. To deal with such type of situations, we need a zero value or a vector of many ways to create a vector with zeros in R. The important thing is the length of the vector.

Examples

> x1<-integer(10)
> x1
[1] 0 0 0 0 0 0 0 0 0 0
> x2<-integer(20)
> x2
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> x3<-numeric(10)
> x3
[1] 0 0 0 0 0 0 0 0 0 0
> x4<-numeric(20)
> x4
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> x5<-replicate(10,0)
> x5
[1] 0 0 0 0 0 0 0 0 0 0
> x6<-replicate(100,0)
> x6
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> x7<-(1:10)*0
> x7
[1] 0 0 0 0 0 0 0 0 0 0
> x8<-(1:50)*0
> x8
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[39] 0 0 0 0 0 0 0 0 0 0 0 0
> x9<-rep(0,10)
> x9
[1] 0 0 0 0 0 0 0 0 0 0
> x10<-rep(0,40)
> x10
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[39] 0 0
> x11<-rep(0L,10)
> x11
[1] 0 0 0 0 0 0 0 0 0 0
> x12<-rep(0L,100)
> x12
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Checking whether each of these objects are vector or not −

> is.vector(x1)
[1] TRUE
> is.vector(x2)
[1] TRUE
> is.vector(x3)
[1] TRUE
> is.vector(x4)
[1] TRUE
> is.vector(x5)
[1] TRUE
> is.vector(x6)
[1] TRUE
> is.vector(x7)
[1] TRUE
> is.vector(x8)
[1] TRUE
> is.vector(x9)
[1] TRUE
> is.vector(x10)
[1] TRUE
> is.vector(x11)
[1] TRUE
> is.vector(x12)
[1] TRUE
raja
Published on 04-Sep-2020 08:23:18
Advertisements