Convert YYYYMM factor format to YYYY-MM format

  • A+
Category:Languages

I have data which have the format of YYYYMM and I wish convert it to YYYY-MM format.

exemple : 201805 should be in the format of 2018-05

How could I do it please ?

 


We can use as.yearmon from zoo to convert it to yearmon object and then do the format

library(zoo) format(as.yearmon(as.character(v1), "%Y%m"), "%Y-%m") #[1] "2018-05" 

data

v1 <- 201805 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: