I have dates in a character vector. I cannot easily convert to a date vector using as.Date, because not all of the strings have the form mm/dd/yyyy, thus giving me the ambiguous date error. Some strings have the form m/dd/yyyy (months 1:9).
Here's part of the vector:
data$Date <- c("8/26/2014","3/10/2014","9/25/2014","11/12/2014","8/4/2015")
Indicator for date to let me know which strings I need to add a zero to
data$date <- grepl("[0-9]{2}/[0-9]{2}/[0-9]{4}", data$Date)
Attempt to add zeros through a conditional:
data$Date<-ifelse(data$date == "FALSE", paste0("0", data$Date), data$Date)
Doesn't work (I'm not familiar with paste). Any concise solutions to add a leading zero to single digit months (m/dd/yyy)? I'm guessing gsub or sub? I need all the strings to be in form mm/dd/yyy so I can convert to a date vector.
data$Date = ifelse(grepl('^[1-9]/', Date), paste0("0", data$Date), data$Date)- eipi10formatstring that tellsasDatehow to parse your date strings. - eipi10