La Machine Infernale La Machine Infernale - 3 months ago 18
R Question

r: convert a string to date

I have a string like that:

201601
201603
201604
201606
201501


And I'd like to convert to Date, like so:

2016-01
2016-03
2016-04
2016-06
2015-01


I have tried:
df$month_key=as.Date(df$month_key,format="YYYYmm")

But it asks for the origin, which we don't need to care about.
Is there a way to do that, or maybe add a dash between character 4 and 5 in the whole column?
Thanks

Answer

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 -.

df1$Col <- sub('(.{4})(.{2})', "\\1-\\2", df1$month_key)
df1$Col
#[1] "2016-01" "2016-03" "2016-04" "2016-06" "2015-01"

Another option is substr/paste

with(df1, paste(substr(month_key, 1,4), substr(month_key, 5, 6), sep="-"))

However, a Date class have a day as well. So, to convert the original column to 'Date', we can append with any day, perhaps with 01 and use the format in as.Date

as.Date(paste0(df1$month_key, "01"), "%Y%m%d")

data

df1 <- structure(list(Col = c(201601L, 201603L, 201604L, 201606L, 201501L
 )), .Names = "month_key", class = "data.frame", row.names = c(NA, -5L))