Search code examples
rvector

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.


Solution

  • 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)]