How to Solve R Error as.Date.numeric(x) : ‘origin’ must be supplied

by | Programming, R, Tips

This error occurs when you try to convert a number to a date without providing an origin date. You can solve this error by providing a date as an origin argument to the as.Date function. For example,

as.Date(34291, origin = "1900-01-01")

This tutorial will go through the error in detail and how to solve it with code examples.


Table of contents

Example

Let’s look at an example of reproducing the error.

# Data frame containing water level of man made pond in cms after n days from creating

df <- data.frame(days_after=c(14, 80, 40, 350),
                 water_level=c(30, 20, 89,  50))

# Print structure of the data frame

str(df)

The str() function returns the structure of the data frame and informs us of the data type in each column:

'data.frame':	4 obs. of  2 variables:
 $ days_after : num  14 80 40 350
 $ water_level: num  30 20 89 50

Both the days_after and water_level columns are numeric. We can use the as.Date method to convert the days_after column to date format.

df$days_after <- as.Date(df$days_after)

Let’s run the code to see what happens:

Error in as.Date.numeric(df$days_after) : 'origin' must be supplied

The error occurred because we did not specify an origin argument for the as.Date method.

If you pass a numeric argument for x to the as.Date method, R interprets x as the number of days since a specified origin.

Solution

We can solve the error by specifying an origin. The origin must be a Date object or something that can be coerced by as.Date(origin, _) to a Date object.

Let’s look at the revised code:

df$days_after <- as.Date(df$days_after, origin="2007-07-04")
df

Let’s run the code to get the result:

 days_after water_level
1 2007-07-18          30
2 2007-09-22          20
3 2007-08-13          89
4 2008-06-18          50

[1] "Date"

When we provide an origin date, the R interpreter converts the numeric values to dates by adding the number of days in the days_after column to the origin. For example,

  • The first row of the days_after column is 14 and the date 2007-07-18 is 14 days after 2007-07-04
  • The second row of the days_after column is 80 and the date 2007-09-22 is 80 days after 2007-07-04

We can confirm that the days_after column has the class Date using the class() function:

class(df$days_after)
[1] "Date"

Summary

Congratulations on reading to the end of this tutorial!

For further reading on R-related errors, go to the articles: 

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!