Select Page

How to Solve R Error in ‘origin’ must be supplied

by | Programming, R, Tips

This error occurs when you try to convert numeric values to Date type using the as.Date() function but do not pass an origin argument to the function.

You can specify an origin, for example:

df$date <- as.Date(df$date, origin='1999-12-31')

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

as.Date in R

In R, as.Date() is a built-in function that converts character representations to objects of class Date to represent calendar dates. If you pass numeric values to the as.Date() function you need to specify the origin.


Consider the following example of a data frame containing the number of pizzas sold by a pizzeria.

df <- data.frame(days=c(1, 5, 10, 15, 30, 50),
pizzas_sold=c(10, 20, 40, 30, 50, 70))


The first column contains the number of days elapsed since the creation of the pizzeria and the second column is the number of pizzas sold.

days pizzas_sold
1    1          10
2    5          20
3   10          40
4   15          30
5   30          50
6   50          70

We can verify that the days column is numeric using the class() function:

[1] "numeric"

Let’s attempt to create a new column in the data frame containing the days elapsed converted to calendar dates:

df$date <-as.Date(df$days)
Error in as.Date.numeric(df$days) : 'origin' must be supplied

The error occurs because the values we passed to as.Date are numeric and require an origin to convert them to Date type objects.


We can solve the error by passing an origin argument to as.Date(). Let’s use the date the pizza makers founded the pizzeria:

df$date <-as.Date(df$days, origin="2021-03-23")

Let’s run the code to get the result.

 days pizzas_sold       date
1    1          10 2021-03-24
2    5          20 2021-03-28
3   10          40 2021-04-02
4   15          30 2021-04-07
5   30          50 2021-04-22
6   50          70 2021-05-12

We can see that the dates match up to the days elapsed after the origin. For example row 2 corresponds to five days elapsed, and five days after 2021-03-23 is 2021-03-28. We can verify that the date column is type Date using the built-in class() function:

[1] "Date"


Congratulations on reading to the end of this tutorial! 

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

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!