0

I have a long vector of date strings that I want to convert to POSIXct objects. When I call as.POSIXct I get this error: Error: character string is not in a standard unambiguous format How do I find which element of the vector caused the error? For example, what would tell me the error in the following code was caused by the second element: as.POSIXct(c('2015-12-10', 'aaa', '2015-12-11'))?

==============

Edit: I'm looking for an answer that's general to vectorized functions in R, as.POSIXct is just an example. In general, when f(x) encounters an error, which element of x caused it?

  • As far as I know, the internals of many vectorized functions won't be able to be exposed to an R user. You could try `?debug` and running through a function if it's written using R code, or use `lapply` to apply a vectorized function one-by-one on a vector and see where the result is unexpected or NULL. – thelatemail Dec 13 '15 at 00:13

1 Answers1

1

If you add a format explicitly to the as.POSIXct() call, you will get NAs returned when a proper date(time) isn't passed in:

as.POSIXct(c('2015-12-10', 'aaa', '2015-12-11'), format="%Y-%m-%d")
#[1] "2015-12-10 EST" NA               "2015-12-11 EST"
thelatemail
  • 91,185
  • 12
  • 128
  • 188
  • I edited my original question to clarify, but what I'm looking for is a general way of finding which element in a vector produced the error. Thanks for the response. – Corned Beef Hash Map Dec 11 '15 at 04:32