Fixing the R error in chartodate(x) : character string is not in a standard unambiguous format

The error in chartodatex : character string is not in a standard unambiguous format error message occurs when you put the wrong date format into the date function. To fix the error you need to fix the format. This is a frequent problem in data science because there are many ways that a person can write a date that does not fit a standard unambiguous format.

Description of the R Error

The date function turns a date in another format into a date object. Like the strptime function, it converts properly formatted character values into a date object. When working with a vector or data frame you must check for missing values. A function call of the isna function will find any na values so that they can be removed. In the case of a data frame, you need to check the date column in each row. Using the date function with a vector or a column can create a time series. If the input data is not consistent with a standard unambiguous format then you will get this error message.

Explanation of the R Error

Here are four examples of code that produces this error message. The fifth example shows another problem that can come from improper date formatting.

> d = “8010”
> d
[1] “8010”
> as.Date(d, origin = “2000-01-01”)
Error in charToDate(x) :
character string is not in a standard unambiguous format

In this example, the current date is eight thousand and ten days after the turn of the century. The problem, however, is that the number is in the form of a character string. As a result, it produces this error message. You will get the same result if you put in a NA value.

> d = factor(“1/15/2006 0:00:00”)
> as.Date(d)
Error in charToDate(x) :
character string is not in a standard unambiguous format

In this example, the data is stored as a factor that has a time, but the date function does not have its format argument. Because of this, the function does not know how to format the factor. As a result, it produces this error message.

> d = “25 Dec 2021”
> as.Date(d)
Error in charToDate(x) :
character string is not in a standard unambiguous format

In this example, the input date string is not formatted properly. It is not in a standard unambiguous format. This format is either year/month/day or year-month-day. Because of the improper formatting, the function produces this error message.

> d = “12/25/2021”
> as.Date(d)
Error in charToDate(x) :
character string is not in a standard unambiguous format

In this example, the input date string is not formatted properly. It is not in a standard unambiguous format. This format is either year/month/day or year-month-day. The date string is month/day/year. Furthermore, because the day is over twelve it cannot be recognized as a month. Because of the improper formatting, the function produces this error message.

> d = “25/12/2021”
> as.Date(d)
[1] “0025-12-20”

In this example, the input date string is not formatted properly. However, it happens to be close enough that it does not produce the error message. In this case, the year and day are switched. This is not a fix because it does not produce a proper date.

How to fix the R Error

Here are four examples on how to fix this error.

> d = 8010
> as.Date(d, origin = “2000-01-01”)
[1] “2021-12-06”

In the first example, we have properly entered the time period as a number. The result is no error message.

> d = factor(“12/25/2021 0:00:00”)
> as.Date(d, format = “%m/%d/%Y”)
[1] “2021-12-25”

In this second example, the factor has a proper format argument and so it produces an output date with no error message.

> d = “2021/12/25”
> as.Date(d)
[1] “2021-12-25”

This final example uses character dates, it is properly formatted with the month in the middle. As a result, it does not produce an error message.

This error message is an easy problem to run into when you do not have control over how data is formatted. Fixing it is a little tricky because there are several different ways it can occur, but once you know how to fix this error will not be a problem.

Scroll to top
Privacy Policy