0

I have a following datasets

enter image description here

I want to convert these rows in date datatype by using Posixct

right now I am using

as.POSIXct(q3_dos_log$INSERTED_ON   , format=c("%d-%mm-%Y"))

but giving wrong output, Kindly solve this.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
sana
  • 15
  • 6

4 Answers4

1

lubridate library is your friend, which can parse many types of dates automatically (if it doesn't, you can specify what format the data is in, to help the package figure it out).

library(lubridate)
x = "28-Sep-2017"
as.POSIXct(parse_date_time(x, "dmy"))
Kim
  • 4,080
  • 2
  • 30
  • 51
  • thanks for your help – sana May 10 '18 at 04:55
  • Glad if it worked out. Feel free to [accept the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if you found it useful. Also it's better if you post your data via `dput` rather than an image in the future. – Kim May 10 '18 at 05:03
1

There is really no need for lubridate here in my opinion; just use base R's as.POSIXct:

as.POSIXct("09-Oct-2017", format = "%d-%b-%Y")
#[1] "2017-10-09 AEDT"

Reformat as "09-10-2017" string:

format(as.POSIXct("09-Oct-2017", format = "%d-%b-%Y"), format = "%d-%m-%Y")
# [1] "09-10-2017"
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
1

We could use anydate from anytime to pick up the formats automatically

anytime::anydate("09-Oct-2017")
#[1] "2017-10-09"
akrun
  • 874,273
  • 37
  • 540
  • 662
1

All you need is as.Date in Base R and knowledge of the formatting options for dates:

as.Date("07-May-2017", format = "%d-%B-%Y")

See more here: https://www.google.se/amp/s/www.r-bloggers.com/date-formats-in-r/amp/

rg255
  • 4,119
  • 3
  • 22
  • 40