If they're strings, does something as simple as
mystr <- c("2012/11/16 00:00:00"," ","")
mystr[grepl("^ *$",mystr)] <- NA
as.Date(mystr)
work? (The regular expression "^ *$"
looks for strings consisting of the start of the string (^
), zero or more spaces (*
), followed by the end of the string ($
). More generally I think you could use "^[[:space:]]*$"
to capture other kinds of whitespace (tabs etc.)