Cenk
Cenk

Reputation: 325

How to insert a new element at the front of a vector?

I have a vector such as this; (1X2406)

head(lnreturn)
[1]           NA  0.004002188  0.003262646 -0.009454616  0.001460387
[6]  0.004005103

I would like to insert an NA as a first element so that I could reach a vector like this:

[1]           NA   NA           0.004002188  0.003262646 -0.009454616          
[6] 0.001460387

Hence, I would get a vector in (1X2407) dimension.

Upvotes: 0

Views: 5486

Answers (2)

zugabe
zugabe

Reputation: 64

its easy (like etienne posted) if you want a vector with same length as a result (like in your example) you can use length().

x<-rnorm(10)
x<-c(NA,x)[1:length(x)]

Upvotes: 2

etienne
etienne

Reputation: 3678

Just use c()

x<-rnorm(10)
x<-c(NA,x)

 x
 [1]           NA -0.004620768  0.760242168  0.038990913  0.735072142 -0.146472627
 [7] -0.057887335  0.482369466  0.992943637 -1.246395498 -0.033487525

Upvotes: 4

Related Questions