R Convert to date from multiple formats

This is pretty much I wrote the anytime package for: R> dates <- c(“01-01-2017″,”02-01-2017″,”12-01-2016″,”20160901″,”20161001”, + “20161101”) R> library(anytime) R> anydate(dates) [1] “2017-01-01” “2017-02-01” “2016-12-01” “2016-09-01” [5] “2016-10-01” “2016-11-01” R> Parse any sane input reliably and without explicit format or origin or other line noise. That being said, not starting ISO style with the year is … Read more

Convert week number to date

as.Date is calling the 1 to 9 as NA as it is expects two digits for the week number and can’t properly parse it. To fix it, add in some – to split things up: as.Date(paste(2014, df$Week, 1, sep=”-“), “%Y-%U-%u”)

What are the “standard unambiguous date” formats for string-to-date conversion in R?

This is documented behavior. From ?as.Date: format: A character string. If not specified, it will try ‘”%Y-%m-%d”‘ then ‘”%Y/%m/%d”‘ on the first non-‘NA’ element, and give an error if neither works. as.Date(“01 Jan 2000”) yields an error because the format isn’t one of the two listed above. as.Date(“01/01/2000”) yields an incorrect answer because the date … Read more

r: convert a string to date [duplicate]

We can use sub to create a – between the first 4 characters and the next 2. Match the four characters (.{4}), place it in a capture groups ((…)), followed by the next 2 characters in another capture group, replace it with the backreference for those groups (\\1, \\2) and in between we add the … Read more