R Error Message: character string is not in a standard unambiguous format

Error messages are a major nuisance to programmers. In fact, if you are not getting error messages you are probably not doing any programming. They can often occur when changing date formats. The character string is not in a standard unambiguous format error message is an easy one to get but even easier to fix.

Description Of The Problem

The character string is not in a standard unambiguous format is a problem that can occur when changing date format using the posixct function. It occurs when converting numeric dates to calendar dates. It is not a problem caused by leading zeros but by using the wrong format in your numeric date. This is why this type of error can be quite tricky. The key to understanding this error is the fact that the numeric date must be a number and not a string. Once you understand this simple detail, you will be able to avoid this problem completely.

Explanation Of The Error

The problem leading to this problem is that the posixct function is looking for a numeric value in the date argument. This error results when you try to enter a character string instead. It will not accept a missing value, but the date variable will round off a decimal number. The function is trying to use the data to calculate calendar dates.

> dates = “1004623500”
> dates
[1] “1004623500”
> as.POSIXct(dates, origin = “1984-01-01”)
Error in as.POSIXlt.character(x, tz, …) :
character string is not in a standard unambiguous formatt

In this example of a situation that causes the problem, you can plainly see that the date variable is being equated to a string and not a number. As a result of this problem, the formula spits out this message.

How To Fix The Problem.

Fixing this problem is quite easy, all that is necessary is to change the input of the date variable as seen below.

> dates = 1004623500
> dates
[1] 1004623500
> as.POSIXct(dates, origin = “1984-01-01”)
[1] “2015-11-01 09:05:00 EST”

As you can see this version produces a date and time. Now if you do not like the format, you can always change it using the strptime or strftime functions. You can also store it in a data frame, character vector, a json file or several other object types. As you can see the results produce a string representation of the day and time although it does not give either daymonth names or month names. There is also no way of encoding dates in byte or Unicode format.

This is an extremely easy problem to avoid, but it is also one that is easy to fall into. To avoid making this mistake it is important to keep in mind that the numerical date needs to be a numerical value. Keep this one fact in mind and you will avoid this problem.

Scroll to top