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.
答案 0 :(得分:3)
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
答案 1 :(得分:1)
很容易(就像etienne发布的那样) 如果你想要一个结果长度相同的向量(比如你的例子),你可以使用length()。
x<-rnorm(10)
x<-c(NA,x)[1:length(x)]