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))
1
solved r: convert a string to date [duplicate]