Corned Beef Hash Map
Corned Beef Hash Map

Reputation: 221

When an R vector function call has an error, which element caused it?

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?

Upvotes: 0

Views: 168

Answers (1)

thelatemail
thelatemail

Reputation: 93813

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"

Upvotes: 1

Related Questions