I have a CSV table that includes a date column containing different formatted date strings. I'll want to write a small function that tries to convert those strings into date objects. The idea is to try different date formats until the date can be parsed:
parseDate <- function(date){
formats <- c("%d.%m.%y %H:%M", "%Y-%m-%d %H:%M:%S", "%m-%d-%Y-%H:%M:%S")
for(format in formats){
newDate <- strptime(date, format=format)
if(!is.na(newDate)){
return(newDate)
}
}
print(paste0("Invalid input: ", date))
return(NA)
}
column1 <- c("2014-11-14 14:20:16", "24.11.14 9:48", "24.11.14 9:49")
table <- data.frame(column1)
table$column1 = lapply(table$column1, FUN = parseDate)
The problem is that I can't find the correct way to apply this function to the column. I tried lapply
, apply
and by
, and non of those functions delivers the desired output. Could you help me here?