我有一个带日期的向量,如下所示:
mydates <- as.Date(c("2004-02-10", "2004-02-12", "2004-02-14", "2004-02-16"))
我想在此向量中添加NA值,我试过这个:
example <- c(rep(NA,2), as.Date(mydates,format="%Y-%m-%d"), rep(NA,3))
但我得到了这个输出:
NA NA 12458 12460 12462 12464 NA NA NA
有人能帮助我吗?
答案 0 :(得分:3)
mydates <- as.Date(c("2004-02-10", "2004-02-12", "2004-02-14", "2004-02-16"))
as.Date(c(rep(NA,2), mydates, rep(NA,3)),origin="1970-01-01")
[1] NA NA "2004-02-10" "2004-02-12" "2004-02-14" "2004-02-16 NA NA NA