如何在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