I'm trying to convert the following dates to a time class in R. For some reason, I am getting NAs returned when a day value is not included. I would like to be able to handle this character vector as is without having to paste an arbitrary day value if possible.
Thanks for your help.
TS <- c("2004-12", "2005-01", "2005-02", "2005-03", "2005-04", "2005-05",
"2005-06", "2005-07", "2005-08", "2005-09", "2005-10", "2005-11",
"2005-12", "2006-01", "2006-02", "2006-03", "2006-04", "2006-05",
"2006-06", "2006-07", "2006-08")
TSd <- paste(TS, "01", sep="-")
#doesn't work
as.Date(TS, format="%Y-%m")
as.POSIXlt(TS, format="%Y-%m")
#works
as.Date(TSd, format="%Y-%m-%d")
as.POSIXlt(TSd, format="%Y-%m-%d")
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…