This answer avoids the subset
, handles missing cases, and uses the as.POSIXct
date / time as.POSIXct
. Although, the rest of the code is pretty much the same as Tyler Rinker's answer. Note that I should specify the name of the date / time variable inside as.POSIXct
, and not use the name of the unformatted variable Date_Time
.
my.data <- read.csv(text = ' Date_Time, state, city 10/05/2011 07:32:40, AK, aa 15/06/2011 13:26:02, AK, bb 19/07/2011 13:26:02, OH, cc NA, OH, dd 20/05/2012 14:57:27, PA, ee 22/07/2012 14:57:27, AL, ff 20/03/2013 15:03:18, NY, gg ', header=TRUE, stringsAsFactors = FALSE, na.strings = 'NA', strip.white = TRUE) my.data$my_Date_Time <- as.POSIXct(my.data$Date_Time, format = "%d/%m/%Y %H:%M:%S")
source share