Neil Neil - 3 months ago 14
R Question

How to convert character into date format in R

I have a csv file which has date in following format.

8/13/2016
8/13/2016
8/13/2016
2016-08-13T08:26:04Z
2016-08-13T14:30:23Z
8/13/2016
8/13/2016


When I import this into R it takes it as a character. I want to convert it into Date format,but when I convert it into date format it takes all NA values

as.Date(df$create_date,format="%m%d%y")


Date field in CSV has different formats in which date is recorded. How can I convert it into date format in R

Answer

A base R option (assuming that there are only two formats in the OP's 'create_date' column), will be to create a logical index with grepl for those date elements that start with 'year', subset the 'create_date' based on the logical index ('i1'), convert to Date class separately and assign those separately to a Date vector of the same length as the number of rows of the dataset to create the full Date class.

i1 <- grepl("^[0-9]{4}", df$create_date)
v1 <- as.Date(df$create_date[i1])
v2 <- as.Date(df$create_date[!i1],  "%m/%d/%Y")
v3 <- Sys.Date() + 0:(nrow(df)-1)   
v3[i1] <- v1
v3[!i1] <- v2
df$create_date <- v3

Or as I commented in the OP's post (first) parse_date_time from lubridate can be used

library(lubridate)
as.Date(parse_date_time(df$create_date, c('mdy', 'ymd_hms')))
#[1] "2016-08-13" "2016-08-13" "2016-08-13" "2016-08-13" 
#[5] "2016-08-13" "2016-08-13" "2016-08-13"

data

df <- structure(list(create_date = c("8/13/2016", "8/13/2016", 
"8/13/2016", 
"2016-08-13T08:26:04Z", "2016-08-13T14:30:23Z", "8/13/2016", 
"8/13/2016")), .Names = "create_date", class = "data.frame",
 row.names = c(NA, -7L))