I have a table, one of the columns is dates in form at 20130109
or YearMonthDay with no spacing.
I'm trying to graph these points, and because of this odd spacing, end up with big gaps in the graph, and thus want to convert these numbers into dates.
I've been trying to use as.Date(as.character(20130107), "%y%m%d")
but that always returns an NA
.
You need %Y
(4 digit year) not %y
(2 digit year):
> as.Date(as.character(20130107), "%Y%m%d")
[1] "07-01-2013"
Using lubridate
library(lubridate)
parse_date_time(as.character(20130107), "%Y%m%d")
If you wanted to just extract certain elements of the date, this is easy with strftime. For example, just the year:
strftime(parse_date_time(as.character(20130107), "%Y%m%d"), "%Y")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With