如何在 R 中创建一个具有零值的向量?
在数据分析中,我们有时需要将零用于某些计算,可能是为了消除变量的影响,也可能是基于分析目标的其他原因。要处理此类情况,我们需要一个零值或零向量。在 R 中有许多方法可以创建具有零值的向量。重要的是向量的长度。
示例
> 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
检查这些对象是否为向量 −
> 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
广告