R: String to Date or NA
Join the DZone community and get the full member experience.Join For Free
I’ve been trying to clean up a CSV file which contains some rows with dates and some not – I only want to keep the cells which do have dates so I’ve been trying to work out how to do that.
My first thought was that I’d try and find a function which would convert the contents of the cell into a date if it was in date format and NA if not. I could then filter out the NA values using the is.na function.
I started out with the as.Date function…
> as.Date("2014-01-01")  "2014-01-01" > as.Date("foo") Error in charToDate(x) : character string is not in a standard unambiguous format
…but that throws an error if we have a non date value so it’s not so useful in this case.
Instead we can make use of the strptime function which does exactly what we want:
> strptime("2014-01-01", "%Y-%m-%d")  "2014-01-01 GMT" > strptime("foo", "%Y-%m-%d")  NA
We can then feed those values into is.na..
> strptime("2014-01-01", "%Y-%m-%d") %>% is.na()  FALSE > strptime("foo", "%Y-%m-%d") %>% is.na()  TRUE
…and we have exactly the behaviour we were looking for.
Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.