Convert Date to Julian Day in R (3 Examples)
In this tutorial, I’ll demonstrate how to convert a date object to a Julian day in R programming.
Table of contents:
So let’s get started:
Creation of Example Data
We use the following data as a basement for this R programming tutorial:
my_date <- as.Date("2023-03-15") # Create example date my_date # Print example date #  "2023-03-15"
The previous output of the RStudio console shows that our example data object is a date with the Date class containing the 15th of March 2023.
Example 1: Convert Calendar Date to Julian Day Using as.POSIXlt() Function
The R programming syntax below illustrates how to transform a date object to a Julian day using the as.POSIXlt function.
Consider the R code below:
my_julian1 <- as.POSIXlt(my_date)$yday # Convert date to Julian day my_julian1 # Print Julian day #  73
The previous syntax has created a new data object called my_julian1 that contains the value 73, i.e. the Julian day corresponding to our example date.
Note that the as.POSIXlt function starts counting Julian days from 0.
Example 2: Convert Calendar Date to Julian Day Using format() Function
Alternatively to the as.POSIXlt function, we can also use the format function to get the Julian day of a date:
my_julian2 <- format(my_date, "%j") # Convert date to Julian day my_julian2 # Print Julian day #  "074"
Note two differences compared to the output of Example 1:
- The format function starts counting Julian days from 1 instead of 0.
- The format function returns a character string instead of a numeric value.
Example 3: Convert Calendar Date to Julian Day Using yday() Function of lubridate Package
So far, we have used Base R to convert our date to a Julian day. This section, in contrast, uses the lubridate package for this task.
First, we need to install and load the lubridate package:
install.packages("lubridate") # Install lubridate package library("lubridate") # Load lubridate
Next, we can apply the yday function to return the Julian day that corresponds to our date:
my_julian3 <- yday(my_date) # Convert date to Julian day my_julian3 # Print Julian day #  74
As you can see, the ymd function starts counting from 1 and returns a numeric value.
Video, Further Resources & Summary
Have a look at the following video on my YouTube channel. I illustrate the topics of this article in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you may have a look at the other tutorials on my website:
- Convert Julian Day to Date in R
- Convert UNIX Timestamp to Date Object
- Convert Data Frame with Date Column to Time Series Object
- Convert Factor to Date in R
- Extract Day from Date in R
- Convert Date to Day of Week
- All R Programming Examples
In this tutorial you have learned how to get a Julian day from a date in the R programming language.
In this tutorial, I have demonstrated how to transform a single date object. Please note that we could apply the same R codes to vectors or entire data frame columns as well.
If you have any additional questions, kindly let me know in the comments below.
Statistics Globe Newsletter