-1

I have a csv file where the date is stored in mm/dd/yyyy format 8/18/2015 12:23:57 PM. When I import this file, it gets imported in string format.

How to convert this to datetime value after importing? I tried to convert while importing, but all the values were as NA.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Kumar
  • 101
  • 6

1 Answers1

2

We can do this with POSIXct

as.POSIXct(str1, format = "%m/%d/%Y %I:%M:%S %p")

data

str1 <- "8/18/2015 12:23:57 PM"
akrun
  • 874,273
  • 37
  • 540
  • 662
  • I am new to R. I am using following code to import csv file. dt <- read.csv("emp.csv") -- col name is stdate , which i want to convert dt will become dataframe. how to use ur commands to convert all the column value. Thanks in advance – Kumar Jul 06 '17 at 11:04
  • 1
    @maankumar Try `dt$myCleanDate <- as.POSIXct(dt$stdate, format = "%m/%d/%Y %I:%M:%S %p")`. Also, invest some time learning [basics of R](https://stackoverflow.com/tags/r/info). – zx8754 Jul 06 '17 at 11:12
  • After doing this all the values became N/A. Any idea what went wrong. – Kumar Jul 06 '17 at 11:48
  • @Kumar The code is based on the format you showed and zx8754's code should work if your format is the same – akrun Jul 06 '17 at 11:53
  • 1
    Thanks it worked. – Kumar Jul 06 '17 at 12:48