-3

I have a Dataset with a column date having following values

> class(daten$TIME)
#[1] "character"
head(daten$TIME)
#[1] "1999M01" "1999M02" "1999M03" "1999M04" "1999M05" "1999M06"

I would like to convert it to Date Format. I tried following code from the package zoom but its showing just NAs

as.yearmon(sub("^M ", "", daten$TIME), "%Y %m")

My sessionInfo() is showing following (maybe it helps):

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252 
lp_ostri
  • 13
  • 2

2 Answers2

0

You can try the code like below

> as.yearmon(s, "%YM%m")
[1] "Jan 1999" "Feb 1999" "Mar 1999" "Apr 1999" "May 1999" "Jun 1999"

> as.yearmon(sub("M", "", s), "%Y%m")
[1] "Jan 1999" "Feb 1999" "Mar 1999" "Apr 1999" "May 1999" "Jun 1999"

Data

s <- c("1999M01", "1999M02", "1999M03", "1999M04", "1999M05", "1999M06")
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
0

If you don't mind installing a new package (using @ThomasIsCoding data):

library(anytime)

anytime(s)

s <- c("1999M01", "1999M02", "1999M03", "1999M04", "1999M05", "1999M06")
bttomio
  • 2,206
  • 1
  • 6
  • 17