I want to convert the following string into year-month?
df <- tribble( ~date, '20201227', ) Here is the desired output.
new_df <- tribble( ~date, '2020-12', ) How can I do this?
Another possible option using gsub (but the as.Date answer by @akrun is more recommended)
transform( df, date = gsub("(\\d{4})(\\d{2}).*", "\\1-\\2", date) ) gives
date 1 2020-12