-1

I have a date.frame and a column with

values(20180213190133, 20180213190136, 20180213190173 , 20180213190193 , 20180213190213, 20180213190233, 20180213190333, 20180213190533, 20180213190733, 20180213190833, 20180213190833, 20180213190833, 201802131901833, 20180213191133, 20180213192133, 20180213194133, 20180213199133, 20180213199133, 20180213199133, 20180213199133, 20180213190136.... 1200 entries)

I want to convert this column which is of type int to Date.

I tried using : as.Date() and as.POSIXct(). Both doesn't work. I am getting N/A value.

Please let me how can I convert this filed from int to Date.

Thanks

patL
  • 2,259
  • 1
  • 17
  • 38
Mirza Hashim
  • 79
  • 11

1 Answers1

0

Try this:

Input data

values<-c(20180213190133, 20180213190136, 20180213190173)

values_date<-as.Date(substr(as.character(values),start = 1,stop=8), format = "%Y%m%d")
> values_date
[1] "2018-02-13" "2018-02-13" "2018-02-13"
> class(values_date)
[1] "Date"

If you want to mantain also hour/minute/second you can try this:

values_date<-as.POSIXlt(as.character(values), format = "%Y%m%d%H%M%S")

After this the class will be "POSIXlt" "POSIXt" and not Date but there are some strange info in your input data

In the third number, last two figures are "73", this number is incorrect for seconds and you will have NA in output.

values_date
[1] "2018-02-13 19:01:33 CET" "2018-02-13 19:01:36 CET" NA 
Terru_theTerror
  • 4,918
  • 2
  • 20
  • 39