The year and month is represented as 201603 representing March,2016. How to convert this numerical value into date format like "March-2016" or "2016-03" in R?
Asked
Active
Viewed 2.7k times
1 Answers
18
A date should always have a given day, therefore consider to add the day at the end of YYYYMM
(in the example below 01
the first day of the month). Convert the numeric
value to character
and the character
to a Date
:
as.Date(paste0(as.character(201603), '01'), format='%Y%m%d')

tic-toc-choc
- 815
- 11
- 26
-
1But it will not solve the problem. The output is "2016-12-01" which is incorrect. – Sanjeev Dec 26 '16 at 08:27
-
Yes %m solved the problem – Sanjeev Dec 26 '16 at 09:42
-
4You can use library(zoo) with following line to get the solution. as.yearmon(as.character(201603), "%Y%m") and it display [1] "Mar 2016" as output in R console. – Saurabh Chauhan Dec 26 '16 at 09:56