I have an example function below that reads in a date as a string and returns it as a date object. If it reads a string that it cannot convert to a date, it returns an error.
testFunction <- function (date_in) {
return(as.Date(date_in))
}
testFunction("2010-04-06") # this works fine
testFunction("foo") # this returns an error
Now, I want to use lapply and apply this function over a list of dates:
dates1 = c("2010-04-06", "2010-04-07", "2010-04-08")
lapply(dates1, testFunction) # this works fine
But if I want to apply the function over a list when one string in the middle of two good dates returns an error, what is the best way to deal with this?
dates2 = c("2010-04-06", "foo", "2010-04-08")
lapply(dates2, testFunction)
I presume that I want a try catch in there, but is there a way to catch the error for the "foo" string whilst asking lapply to continue and read the third date?
One could try to keep it simple rather than to make it complicated:
You can trivially wrap
na.omit()
or whatever around it. Or find the index of NAs and extract accordingly from the initial vector, or use the complement of the NAs to find the parsed dates, or, or, or. It is all here already.You can make your
testFunction()
do something. Use the test there -- if the returned (parsed) date is NA, do something.Add a
tryCatch()
block or atry()
to your date parsing.The whole things is a little odd as you go from a one-type data structure (vector of chars) to something else, but you can't easily mix types unless you keep them in a
list
type. So maybe you need to rethink this.Use a
tryCatch
expression around the function that can throw the error message:The nice thing about the
tryCatch
function is that you can decide what to do in the case of an error (in this case, returnNULL
).Assuming the
testFunction()
is not trivial and/or that one cannot alter it, it can be wrapped in a function of your own, with a tryCatch() block. For example: