Tuesday, 15 June 2010

r - Convert numeric values to dates -



r - Convert numeric values to dates -

i have numeric vector follows

aa <- c(1022011, 2022011, 13022011, 23022011) (this vector sample, long)

values written in such way first value day month , year.

what doing right

as.date(as.character(aa), %d%m%y")

but,

it causing problems (returning na) in case of single digits day numbers. (i.e. 1022011, 2022011).

so

as.date("1022011", "%d%m%y") not work

but

as.date("01022011", "%d%m%y") (pasting '0' ahead of number) works.

i want avoid pasting '0' in such cases. there other (direct) alternative convert numeric values dates @ once?

depending on platform, utilize sprintf in order add together 0 @ beginning. seems mac ok this, not windows 7 given give-and-take op.

aa <- c(1022011, 2022011, 13022011, 23022011) as.date(sprintf("%08s", aa), format = "%d%m%y") [1] "2011-02-01" "2011-02-02" "2011-02-13" "2011-02-23"

update

@cathyg kindly mentioned sprintf("%08i",aa) works on windows 7.

r date datetime zoo lubridate

No comments:

Post a Comment